ip17xx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. * ip17xx.c: Swconfig configuration for IC+ IP17xx switch family
  3. *
  4. * Copyright (C) 2008 Patrick Horn <patrick.horn@gmail.com>
  5. * Copyright (C) 2008, 2010 Martin Mares <mj@ucw.cz>
  6. * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/mii.h>
  24. #include <linux/phy.h>
  25. #include <linux/delay.h>
  26. #include <linux/switch.h>
  27. #include <linux/device.h>
  28. #define MAX_VLANS 16
  29. #define MAX_PORTS 9
  30. #undef DUMP_MII_IO
  31. typedef struct ip17xx_reg {
  32. u16 p; // phy
  33. u16 m; // mii
  34. } reg;
  35. typedef char bitnum;
  36. #define NOTSUPPORTED {-1,-1}
  37. #define REG_SUPP(x) (((x).m != ((u16)-1)) && ((x).p != (u16)-1))
  38. struct ip17xx_state;
  39. /*********** CONSTANTS ***********/
  40. struct register_mappings {
  41. char *NAME;
  42. u16 MODEL_NO; // Compare to bits 4-9 of MII register 0,3.
  43. bitnum NUM_PORTS;
  44. bitnum CPU_PORT;
  45. /* The default VLAN for each port.
  46. Default: 0x0001 for Ports 0,1,2,3
  47. 0x0002 for Ports 4,5 */
  48. reg VLAN_DEFAULT_TAG_REG[MAX_PORTS];
  49. /* These ports are tagged.
  50. Default: 0x00 */
  51. reg ADD_TAG_REG;
  52. reg REMOVE_TAG_REG;
  53. bitnum ADD_TAG_BIT[MAX_PORTS];
  54. /* These ports are untagged.
  55. Default: 0x00 (i.e. do not alter any VLAN tags...)
  56. Maybe set to 0 if user disables VLANs. */
  57. bitnum REMOVE_TAG_BIT[MAX_PORTS];
  58. /* Port M and Port N are on the same VLAN.
  59. Default: All ports on all VLANs. */
  60. // Use register {29, 19+N/2}
  61. reg VLAN_LOOKUP_REG;
  62. // Port 5 uses register {30, 18} but same as odd bits.
  63. reg VLAN_LOOKUP_REG_5; // in a different register on IP175C.
  64. bitnum VLAN_LOOKUP_EVEN_BIT[MAX_PORTS];
  65. bitnum VLAN_LOOKUP_ODD_BIT[MAX_PORTS];
  66. /* This VLAN corresponds to which ports.
  67. Default: 0x2f,0x30,0x3f,0x3f... */
  68. reg TAG_VLAN_MASK_REG;
  69. bitnum TAG_VLAN_MASK_EVEN_BIT[MAX_PORTS];
  70. bitnum TAG_VLAN_MASK_ODD_BIT[MAX_PORTS];
  71. int RESET_VAL;
  72. reg RESET_REG;
  73. reg MODE_REG;
  74. int MODE_VAL;
  75. /* General flags */
  76. reg ROUTER_CONTROL_REG;
  77. reg VLAN_CONTROL_REG;
  78. bitnum TAG_VLAN_BIT;
  79. bitnum ROUTER_EN_BIT;
  80. bitnum NUMLAN_GROUPS_MAX;
  81. bitnum NUMLAN_GROUPS_BIT;
  82. reg MII_REGISTER_EN;
  83. bitnum MII_REGISTER_EN_BIT;
  84. // set to 1 for 178C, 0 for 175C.
  85. bitnum SIMPLE_VLAN_REGISTERS; // 175C has two vlans per register but 178C has only one.
  86. // Pointers to functions which manipulate hardware state
  87. int (*update_state)(struct ip17xx_state *state);
  88. int (*set_vlan_mode)(struct ip17xx_state *state);
  89. int (*reset)(struct ip17xx_state *state);
  90. };
  91. static int ip175c_update_state(struct ip17xx_state *state);
  92. static int ip175c_set_vlan_mode(struct ip17xx_state *state);
  93. static int ip175c_reset(struct ip17xx_state *state);
  94. static const struct register_mappings IP178C = {
  95. .NAME = "IP178C",
  96. .MODEL_NO = 0x18,
  97. .VLAN_DEFAULT_TAG_REG = {
  98. {30,3},{30,4},{30,5},{30,6},{30,7},{30,8},
  99. {30,9},{30,10},{30,11},
  100. },
  101. .ADD_TAG_REG = {30,12},
  102. .ADD_TAG_BIT = {0,1,2,3,4,5,6,7,8},
  103. .REMOVE_TAG_REG = {30,13},
  104. .REMOVE_TAG_BIT = {4,5,6,7,8,9,10,11,12},
  105. .SIMPLE_VLAN_REGISTERS = 1,
  106. .VLAN_LOOKUP_REG = {31,0},// +N
  107. .VLAN_LOOKUP_REG_5 = NOTSUPPORTED, // not used with SIMPLE_VLAN_REGISTERS
  108. .VLAN_LOOKUP_EVEN_BIT = {0,1,2,3,4,5,6,7,8},
  109. .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,5,6,7,8},
  110. .TAG_VLAN_MASK_REG = {30,14}, // +N
  111. .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,6,7,8},
  112. .TAG_VLAN_MASK_ODD_BIT = {0,1,2,3,4,5,6,7,8},
  113. .RESET_VAL = 0x55AA,
  114. .RESET_REG = {30,0},
  115. .MODE_VAL = 0,
  116. .MODE_REG = NOTSUPPORTED,
  117. .ROUTER_CONTROL_REG = {30,30},
  118. .ROUTER_EN_BIT = 11,
  119. .NUMLAN_GROUPS_MAX = 8,
  120. .NUMLAN_GROUPS_BIT = 8, // {0-2}
  121. .VLAN_CONTROL_REG = {30,13},
  122. .TAG_VLAN_BIT = 3,
  123. .CPU_PORT = 8,
  124. .NUM_PORTS = 9,
  125. .MII_REGISTER_EN = NOTSUPPORTED,
  126. .update_state = ip175c_update_state,
  127. .set_vlan_mode = ip175c_set_vlan_mode,
  128. .reset = ip175c_reset,
  129. };
  130. static const struct register_mappings IP175C = {
  131. .NAME = "IP175C",
  132. .MODEL_NO = 0x18,
  133. .VLAN_DEFAULT_TAG_REG = {
  134. {29,24},{29,25},{29,26},{29,27},{29,28},{29,30},
  135. NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED
  136. },
  137. .ADD_TAG_REG = {29,23},
  138. .REMOVE_TAG_REG = {29,23},
  139. .ADD_TAG_BIT = {11,12,13,14,15,1,-1,-1,-1},
  140. .REMOVE_TAG_BIT = {6,7,8,9,10,0,-1,-1,-1},
  141. .SIMPLE_VLAN_REGISTERS = 0,
  142. .VLAN_LOOKUP_REG = {29,19},// +N/2
  143. .VLAN_LOOKUP_REG_5 = {30,18},
  144. .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,15,-1,-1,-1},
  145. .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,7,-1,-1,-1},
  146. .TAG_VLAN_MASK_REG = {30,1}, // +N/2
  147. .TAG_VLAN_MASK_EVEN_BIT = {0,1,2,3,4,5,-1,-1,-1},
  148. .TAG_VLAN_MASK_ODD_BIT = {8,9,10,11,12,13,-1,-1,-1},
  149. .RESET_VAL = 0x175C,
  150. .RESET_REG = {30,0},
  151. .MODE_VAL = 0x175C,
  152. .MODE_REG = {29,31},
  153. .ROUTER_CONTROL_REG = {30,9},
  154. .ROUTER_EN_BIT = 3,
  155. .NUMLAN_GROUPS_MAX = 8,
  156. .NUMLAN_GROUPS_BIT = 0, // {0-2}
  157. .VLAN_CONTROL_REG = {30,9},
  158. .TAG_VLAN_BIT = 7,
  159. .NUM_PORTS = 6,
  160. .CPU_PORT = 5,
  161. .MII_REGISTER_EN = NOTSUPPORTED,
  162. .update_state = ip175c_update_state,
  163. .set_vlan_mode = ip175c_set_vlan_mode,
  164. .reset = ip175c_reset,
  165. };
  166. static const struct register_mappings IP175A = {
  167. .NAME = "IP175A",
  168. .MODEL_NO = 0x05,
  169. .VLAN_DEFAULT_TAG_REG = {
  170. {0,24},{0,25},{0,26},{0,27},{0,28},NOTSUPPORTED,
  171. NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED
  172. },
  173. .ADD_TAG_REG = {0,23},
  174. .REMOVE_TAG_REG = {0,23},
  175. .ADD_TAG_BIT = {11,12,13,14,15,-1,-1,-1,-1},
  176. .REMOVE_TAG_BIT = {6,7,8,9,10,-1,-1,-1,-1},
  177. .SIMPLE_VLAN_REGISTERS = 0,
  178. // Only programmable via EEPROM
  179. .VLAN_LOOKUP_REG = NOTSUPPORTED,// +N/2
  180. .VLAN_LOOKUP_REG_5 = NOTSUPPORTED,
  181. .VLAN_LOOKUP_EVEN_BIT = {8,9,10,11,12,-1,-1,-1,-1},
  182. .VLAN_LOOKUP_ODD_BIT = {0,1,2,3,4,-1,-1,-1,-1},
  183. .TAG_VLAN_MASK_REG = NOTSUPPORTED, // +N/2,
  184. .TAG_VLAN_MASK_EVEN_BIT = {-1,-1,-1,-1,-1,-1,-1,-1,-1},
  185. .TAG_VLAN_MASK_ODD_BIT = {-1,-1,-1,-1,-1,-1,-1,-1,-1},
  186. .RESET_VAL = -1,
  187. .RESET_REG = NOTSUPPORTED,
  188. .MODE_VAL = 0,
  189. .MODE_REG = NOTSUPPORTED,
  190. .ROUTER_CONTROL_REG = NOTSUPPORTED,
  191. .VLAN_CONTROL_REG = NOTSUPPORTED,
  192. .TAG_VLAN_BIT = -1,
  193. .ROUTER_EN_BIT = -1,
  194. .NUMLAN_GROUPS_MAX = -1,
  195. .NUMLAN_GROUPS_BIT = -1, // {0-2}
  196. .NUM_PORTS = 5,
  197. .CPU_PORT = 4,
  198. .MII_REGISTER_EN = {0, 18},
  199. .MII_REGISTER_EN_BIT = 7,
  200. .update_state = ip175c_update_state,
  201. .set_vlan_mode = ip175c_set_vlan_mode,
  202. .reset = ip175c_reset,
  203. };
  204. static int ip175d_update_state(struct ip17xx_state *state);
  205. static int ip175d_set_vlan_mode(struct ip17xx_state *state);
  206. static int ip175d_reset(struct ip17xx_state *state);
  207. static const struct register_mappings IP175D = {
  208. .NAME = "IP175D",
  209. .MODEL_NO = 0x18,
  210. // The IP175D has a completely different interface, so we leave most
  211. // of the registers undefined and switch to different code paths.
  212. .VLAN_DEFAULT_TAG_REG = {
  213. NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED,
  214. NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED,NOTSUPPORTED,
  215. },
  216. .ADD_TAG_REG = NOTSUPPORTED,
  217. .REMOVE_TAG_REG = NOTSUPPORTED,
  218. .SIMPLE_VLAN_REGISTERS = 0,
  219. .VLAN_LOOKUP_REG = NOTSUPPORTED,
  220. .VLAN_LOOKUP_REG_5 = NOTSUPPORTED,
  221. .TAG_VLAN_MASK_REG = NOTSUPPORTED,
  222. .RESET_VAL = 0x175D,
  223. .RESET_REG = {20,2},
  224. .MODE_REG = NOTSUPPORTED,
  225. .ROUTER_CONTROL_REG = NOTSUPPORTED,
  226. .ROUTER_EN_BIT = -1,
  227. .NUMLAN_GROUPS_BIT = -1,
  228. .VLAN_CONTROL_REG = NOTSUPPORTED,
  229. .TAG_VLAN_BIT = -1,
  230. .NUM_PORTS = 6,
  231. .CPU_PORT = 5,
  232. .MII_REGISTER_EN = NOTSUPPORTED,
  233. .update_state = ip175d_update_state,
  234. .set_vlan_mode = ip175d_set_vlan_mode,
  235. .reset = ip175d_reset,
  236. };
  237. struct ip17xx_state {
  238. struct switch_dev dev;
  239. struct mii_bus *mii_bus;
  240. bool registered;
  241. int router_mode; // ROUTER_EN
  242. int vlan_enabled; // TAG_VLAN_EN
  243. struct port_state {
  244. u16 pvid;
  245. unsigned int shareports;
  246. } ports[MAX_PORTS];
  247. unsigned int add_tag;
  248. unsigned int remove_tag;
  249. int num_vlans;
  250. struct vlan_state {
  251. unsigned int ports;
  252. unsigned int tag; // VLAN tag (IP175D only)
  253. } vlans[MAX_VLANS];
  254. const struct register_mappings *regs;
  255. reg proc_mii; // phy/reg for the low level register access via swconfig
  256. char buf[80];
  257. };
  258. #define get_state(_dev) container_of((_dev), struct ip17xx_state, dev)
  259. static int ip_phy_read(struct ip17xx_state *state, int port, int reg)
  260. {
  261. int val = mdiobus_read(state->mii_bus, port, reg);
  262. if (val < 0)
  263. pr_warning("IP17xx: Unable to get MII register %d,%d: error %d\n", port, reg, -val);
  264. #ifdef DUMP_MII_IO
  265. else
  266. pr_debug("IP17xx: Read MII(%d,%d) -> %04x\n", port, reg, val);
  267. #endif
  268. return val;
  269. }
  270. static int ip_phy_write(struct ip17xx_state *state, int port, int reg, u16 val)
  271. {
  272. int err;
  273. #ifdef DUMP_MII_IO
  274. pr_debug("IP17xx: Write MII(%d,%d) <- %04x\n", port, reg, val);
  275. #endif
  276. err = mdiobus_write(state->mii_bus, port, reg, val);
  277. if (err < 0)
  278. pr_warning("IP17xx: Unable to write MII register %d,%d: error %d\n", port, reg, -err);
  279. return err;
  280. }
  281. static int ip_phy_write_masked(struct ip17xx_state *state, int port, int reg, unsigned int mask, unsigned int data)
  282. {
  283. int val = ip_phy_read(state, port, reg);
  284. if (val < 0)
  285. return 0;
  286. return ip_phy_write(state, port, reg, (val & ~mask) | data);
  287. }
  288. static int getPhy(struct ip17xx_state *state, reg mii)
  289. {
  290. if (!REG_SUPP(mii))
  291. return -EFAULT;
  292. return ip_phy_read(state, mii.p, mii.m);
  293. }
  294. static int setPhy(struct ip17xx_state *state, reg mii, u16 value)
  295. {
  296. int err;
  297. if (!REG_SUPP(mii))
  298. return -EFAULT;
  299. err = ip_phy_write(state, mii.p, mii.m, value);
  300. if (err < 0)
  301. return err;
  302. mdelay(2);
  303. getPhy(state, mii);
  304. return 0;
  305. }
  306. /**
  307. * These two macros are to simplify the mapping of logical bits to the bits in hardware.
  308. * NOTE: these macros will return if there is an error!
  309. */
  310. #define GET_PORT_BITS(state, bits, addr, bit_lookup) \
  311. do { \
  312. int i, val = getPhy((state), (addr)); \
  313. if (val < 0) \
  314. return val; \
  315. (bits) = 0; \
  316. for (i = 0; i < MAX_PORTS; i++) { \
  317. if ((bit_lookup)[i] == -1) continue; \
  318. if (val & (1<<(bit_lookup)[i])) \
  319. (bits) |= (1<<i); \
  320. } \
  321. } while (0)
  322. #define SET_PORT_BITS(state, bits, addr, bit_lookup) \
  323. do { \
  324. int i, val = getPhy((state), (addr)); \
  325. if (val < 0) \
  326. return val; \
  327. for (i = 0; i < MAX_PORTS; i++) { \
  328. unsigned int newmask = ((bits)&(1<<i)); \
  329. if ((bit_lookup)[i] == -1) continue; \
  330. val &= ~(1<<(bit_lookup)[i]); \
  331. val |= ((newmask>>i)<<(bit_lookup)[i]); \
  332. } \
  333. val = setPhy((state), (addr), val); \
  334. if (val < 0) \
  335. return val; \
  336. } while (0)
  337. static int get_model(struct ip17xx_state *state)
  338. {
  339. int id1, id2;
  340. int oui_id, model_no, rev_no, chip_no;
  341. id1 = ip_phy_read(state, 0, 2);
  342. id2 = ip_phy_read(state, 0, 3);
  343. oui_id = (id1 << 6) | ((id2 >> 10) & 0x3f);
  344. model_no = (id2 >> 4) & 0x3f;
  345. rev_no = id2 & 0xf;
  346. pr_debug("IP17xx: Identified oui=%06x model=%02x rev=%X\n", oui_id, model_no, rev_no);
  347. if (oui_id != 0x0090c3) // No other oui_id should have reached us anyway
  348. return -ENODEV;
  349. if (model_no == IP175A.MODEL_NO) {
  350. state->regs = &IP175A;
  351. } else if (model_no == IP175C.MODEL_NO) {
  352. /*
  353. * Several models share the same model_no:
  354. * 178C has more PHYs, so we try whether the device responds to a read from PHY5
  355. * 175D has a new chip ID register
  356. * 175C has neither
  357. */
  358. if (ip_phy_read(state, 5, 2) == 0x0243) {
  359. state->regs = &IP178C;
  360. } else {
  361. chip_no = ip_phy_read(state, 20, 0);
  362. pr_debug("IP17xx: Chip ID register reads %04x\n", chip_no);
  363. if (chip_no == 0x175d) {
  364. state->regs = &IP175D;
  365. } else {
  366. state->regs = &IP175C;
  367. }
  368. }
  369. } else {
  370. pr_warning("IP17xx: Found an unknown IC+ switch with model number %02x, revision %X.\n", model_no, rev_no);
  371. return -EPERM;
  372. }
  373. return 0;
  374. }
  375. /*** Low-level functions for the older models ***/
  376. /** Only set vlan and router flags in the switch **/
  377. static int ip175c_set_flags(struct ip17xx_state *state)
  378. {
  379. int val;
  380. if (!REG_SUPP(state->regs->ROUTER_CONTROL_REG)) {
  381. return 0;
  382. }
  383. val = getPhy(state, state->regs->ROUTER_CONTROL_REG);
  384. if (val < 0) {
  385. return val;
  386. }
  387. if (state->regs->ROUTER_EN_BIT >= 0) {
  388. if (state->router_mode) {
  389. val |= (1<<state->regs->ROUTER_EN_BIT);
  390. } else {
  391. val &= (~(1<<state->regs->ROUTER_EN_BIT));
  392. }
  393. }
  394. if (state->regs->TAG_VLAN_BIT >= 0) {
  395. if (state->vlan_enabled) {
  396. val |= (1<<state->regs->TAG_VLAN_BIT);
  397. } else {
  398. val &= (~(1<<state->regs->TAG_VLAN_BIT));
  399. }
  400. }
  401. if (state->regs->NUMLAN_GROUPS_BIT >= 0) {
  402. val &= (~((state->regs->NUMLAN_GROUPS_MAX-1)<<state->regs->NUMLAN_GROUPS_BIT));
  403. if (state->num_vlans > state->regs->NUMLAN_GROUPS_MAX) {
  404. val |= state->regs->NUMLAN_GROUPS_MAX << state->regs->NUMLAN_GROUPS_BIT;
  405. } else if (state->num_vlans >= 1) {
  406. val |= (state->num_vlans-1) << state->regs->NUMLAN_GROUPS_BIT;
  407. }
  408. }
  409. return setPhy(state, state->regs->ROUTER_CONTROL_REG, val);
  410. }
  411. /** Set all VLAN and port state. Usually you should call "correct_vlan_state" first. **/
  412. static int ip175c_set_state(struct ip17xx_state *state)
  413. {
  414. int j;
  415. int i;
  416. SET_PORT_BITS(state, state->add_tag,
  417. state->regs->ADD_TAG_REG, state->regs->ADD_TAG_BIT);
  418. SET_PORT_BITS(state, state->remove_tag,
  419. state->regs->REMOVE_TAG_REG, state->regs->REMOVE_TAG_BIT);
  420. if (REG_SUPP(state->regs->VLAN_LOOKUP_REG)) {
  421. for (j=0; j<state->regs->NUM_PORTS; j++) {
  422. reg addr;
  423. const bitnum *bit_lookup = (j%2==0)?
  424. state->regs->VLAN_LOOKUP_EVEN_BIT:
  425. state->regs->VLAN_LOOKUP_ODD_BIT;
  426. addr = state->regs->VLAN_LOOKUP_REG;
  427. if (state->regs->SIMPLE_VLAN_REGISTERS) {
  428. addr.m += j;
  429. } else {
  430. switch (j) {
  431. case 0:
  432. case 1:
  433. break;
  434. case 2:
  435. case 3:
  436. addr.m+=1;
  437. break;
  438. case 4:
  439. addr.m+=2;
  440. break;
  441. case 5:
  442. addr = state->regs->VLAN_LOOKUP_REG_5;
  443. break;
  444. default:
  445. addr.m = -1; // shouldn't get here, but...
  446. break;
  447. }
  448. }
  449. //printf("shareports for %d is %02X\n",j,state->ports[j].shareports);
  450. if (REG_SUPP(addr)) {
  451. SET_PORT_BITS(state, state->ports[j].shareports, addr, bit_lookup);
  452. }
  453. }
  454. }
  455. if (REG_SUPP(state->regs->TAG_VLAN_MASK_REG)) {
  456. for (j=0; j<MAX_VLANS; j++) {
  457. reg addr = state->regs->TAG_VLAN_MASK_REG;
  458. const bitnum *bit_lookup = (j%2==0)?
  459. state->regs->TAG_VLAN_MASK_EVEN_BIT:
  460. state->regs->TAG_VLAN_MASK_ODD_BIT;
  461. unsigned int vlan_mask;
  462. if (state->regs->SIMPLE_VLAN_REGISTERS) {
  463. addr.m += j;
  464. } else {
  465. addr.m += j/2;
  466. }
  467. vlan_mask = state->vlans[j].ports;
  468. SET_PORT_BITS(state, vlan_mask, addr, bit_lookup);
  469. }
  470. }
  471. for (i=0; i<MAX_PORTS; i++) {
  472. if (REG_SUPP(state->regs->VLAN_DEFAULT_TAG_REG[i])) {
  473. int err = setPhy(state, state->regs->VLAN_DEFAULT_TAG_REG[i],
  474. state->ports[i].pvid);
  475. if (err < 0) {
  476. return err;
  477. }
  478. }
  479. }
  480. return ip175c_set_flags(state);
  481. }
  482. /**
  483. * Uses only the VLAN port mask and the add tag mask to generate the other fields:
  484. * which ports are part of the same VLAN, removing vlan tags, and VLAN tag ids.
  485. */
  486. static void ip175c_correct_vlan_state(struct ip17xx_state *state)
  487. {
  488. int i, j;
  489. state->num_vlans = 0;
  490. for (i=0; i<MAX_VLANS; i++) {
  491. if (state->vlans[i].ports != 0) {
  492. state->num_vlans = i+1; // Hack -- we need to store the "set" vlans somewhere...
  493. }
  494. }
  495. for (i=0; i<state->regs->NUM_PORTS; i++) {
  496. unsigned int portmask = (1<<i);
  497. if (!state->vlan_enabled) {
  498. // Share with everybody!
  499. state->ports[i].shareports = (1<<state->regs->NUM_PORTS)-1;
  500. continue;
  501. }
  502. state->ports[i].shareports = portmask;
  503. for (j=0; j<MAX_VLANS; j++) {
  504. if (state->vlans[j].ports & portmask)
  505. state->ports[i].shareports |= state->vlans[j].ports;
  506. }
  507. }
  508. }
  509. static int ip175c_update_state(struct ip17xx_state *state)
  510. {
  511. ip175c_correct_vlan_state(state);
  512. return ip175c_set_state(state);
  513. }
  514. static int ip175c_set_vlan_mode(struct ip17xx_state *state)
  515. {
  516. return ip175c_update_state(state);
  517. }
  518. static int ip175c_reset(struct ip17xx_state *state)
  519. {
  520. int err;
  521. if (REG_SUPP(state->regs->MODE_REG)) {
  522. err = setPhy(state, state->regs->MODE_REG, state->regs->MODE_VAL);
  523. if (err < 0)
  524. return err;
  525. err = getPhy(state, state->regs->MODE_REG);
  526. if (err < 0)
  527. return err;
  528. }
  529. return ip175c_update_state(state);
  530. }
  531. /*** Low-level functions for IP175D ***/
  532. static int ip175d_update_state(struct ip17xx_state *state)
  533. {
  534. unsigned int filter_mask = 0;
  535. unsigned int ports[16], add[16], rem[16];
  536. int i, j;
  537. int err = 0;
  538. for (i = 0; i < 16; i++) {
  539. ports[i] = 0;
  540. add[i] = 0;
  541. rem[i] = 0;
  542. if (!state->vlan_enabled) {
  543. err |= ip_phy_write(state, 22, 14+i, i+1); // default tags
  544. ports[i] = 0x3f;
  545. continue;
  546. }
  547. if (!state->vlans[i].tag) {
  548. // Reset the filter
  549. err |= ip_phy_write(state, 22, 14+i, 0); // tag
  550. continue;
  551. }
  552. filter_mask |= 1 << i;
  553. err |= ip_phy_write(state, 22, 14+i, state->vlans[i].tag);
  554. ports[i] = state->vlans[i].ports;
  555. for (j = 0; j < 6; j++) {
  556. if (ports[i] & (1 << j)) {
  557. if (state->add_tag & (1 << j))
  558. add[i] |= 1 << j;
  559. if (state->remove_tag & (1 << j))
  560. rem[i] |= 1 << j;
  561. }
  562. }
  563. }
  564. // Port masks, tag adds and removals
  565. for (i = 0; i < 8; i++) {
  566. err |= ip_phy_write(state, 23, i, ports[2*i] | (ports[2*i+1] << 8));
  567. err |= ip_phy_write(state, 23, 8+i, add[2*i] | (add[2*i+1] << 8));
  568. err |= ip_phy_write(state, 23, 16+i, rem[2*i] | (rem[2*i+1] << 8));
  569. }
  570. err |= ip_phy_write(state, 22, 10, filter_mask);
  571. // Default VLAN tag for each port
  572. for (i = 0; i < 6; i++)
  573. err |= ip_phy_write(state, 22, 4+i, state->vlans[state->ports[i].pvid].tag);
  574. return (err ? -EIO : 0);
  575. }
  576. static int ip175d_set_vlan_mode(struct ip17xx_state *state)
  577. {
  578. int i;
  579. int err = 0;
  580. if (state->vlan_enabled) {
  581. // VLAN classification rules: tag-based VLANs, use VID to classify,
  582. // drop packets that cannot be classified.
  583. err |= ip_phy_write_masked(state, 22, 0, 0x3fff, 0x003f);
  584. // Ingress rules: CFI=1 dropped, null VID is untagged, VID=1 passed,
  585. // VID=0xfff discarded, admin both tagged and untagged, ingress
  586. // filters enabled.
  587. err |= ip_phy_write_masked(state, 22, 1, 0x0fff, 0x0c3f);
  588. // Egress rules: IGMP processing off, keep VLAN header off
  589. err |= ip_phy_write_masked(state, 22, 2, 0x0fff, 0x0000);
  590. } else {
  591. // VLAN classification rules: everything off & clear table
  592. err |= ip_phy_write_masked(state, 22, 0, 0xbfff, 0x8000);
  593. // Ingress and egress rules: set to defaults
  594. err |= ip_phy_write_masked(state, 22, 1, 0x0fff, 0x0c3f);
  595. err |= ip_phy_write_masked(state, 22, 2, 0x0fff, 0x0000);
  596. }
  597. // Reset default VLAN for each port to 0
  598. for (i = 0; i < 6; i++)
  599. state->ports[i].pvid = 0;
  600. err |= ip175d_update_state(state);
  601. return (err ? -EIO : 0);
  602. }
  603. static int ip175d_reset(struct ip17xx_state *state)
  604. {
  605. int err = 0;
  606. // Disable the special tagging mode
  607. err |= ip_phy_write_masked(state, 21, 22, 0x0003, 0x0000);
  608. // Set 802.1q protocol type
  609. err |= ip_phy_write(state, 22, 3, 0x8100);
  610. state->vlan_enabled = 0;
  611. err |= ip175d_set_vlan_mode(state);
  612. return (err ? -EIO : 0);
  613. }
  614. /*** High-level functions ***/
  615. static int ip17xx_get_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  616. {
  617. struct ip17xx_state *state = get_state(dev);
  618. val->value.i = state->vlan_enabled;
  619. return 0;
  620. }
  621. static void ip17xx_reset_vlan_config(struct ip17xx_state *state)
  622. {
  623. int i;
  624. state->remove_tag = (state->vlan_enabled ? ((1<<state->regs->NUM_PORTS)-1) : 0x0000);
  625. state->add_tag = 0x0000;
  626. for (i = 0; i < MAX_VLANS; i++) {
  627. state->vlans[i].ports = 0x0000;
  628. state->vlans[i].tag = (i ? i : 16);
  629. }
  630. for (i = 0; i < MAX_PORTS; i++)
  631. state->ports[i].pvid = 0;
  632. }
  633. static int ip17xx_set_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  634. {
  635. struct ip17xx_state *state = get_state(dev);
  636. int enable;
  637. enable = val->value.i;
  638. if (state->vlan_enabled == enable) {
  639. // Do not change any state.
  640. return 0;
  641. }
  642. state->vlan_enabled = enable;
  643. // Otherwise, if we are switching state, set fields to a known default.
  644. ip17xx_reset_vlan_config(state);
  645. return state->regs->set_vlan_mode(state);
  646. }
  647. static int ip17xx_get_ports(struct switch_dev *dev, struct switch_val *val)
  648. {
  649. struct ip17xx_state *state = get_state(dev);
  650. int b;
  651. int ind;
  652. unsigned int ports;
  653. if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
  654. return -EINVAL;
  655. ports = state->vlans[val->port_vlan].ports;
  656. b = 0;
  657. ind = 0;
  658. while (b < MAX_PORTS) {
  659. if (ports&1) {
  660. int istagged = ((state->add_tag >> b) & 1);
  661. val->value.ports[ind].id = b;
  662. val->value.ports[ind].flags = (istagged << SWITCH_PORT_FLAG_TAGGED);
  663. ind++;
  664. }
  665. b++;
  666. ports >>= 1;
  667. }
  668. val->len = ind;
  669. return 0;
  670. }
  671. static int ip17xx_set_ports(struct switch_dev *dev, struct switch_val *val)
  672. {
  673. struct ip17xx_state *state = get_state(dev);
  674. int i;
  675. if (val->port_vlan >= dev->vlans || val->port_vlan < 0)
  676. return -EINVAL;
  677. state->vlans[val->port_vlan].ports = 0;
  678. for (i = 0; i < val->len; i++) {
  679. unsigned int bitmask = (1<<val->value.ports[i].id);
  680. state->vlans[val->port_vlan].ports |= bitmask;
  681. if (val->value.ports[i].flags & (1<<SWITCH_PORT_FLAG_TAGGED)) {
  682. state->add_tag |= bitmask;
  683. state->remove_tag &= (~bitmask);
  684. } else {
  685. state->add_tag &= (~bitmask);
  686. state->remove_tag |= bitmask;
  687. }
  688. }
  689. return state->regs->update_state(state);
  690. }
  691. static int ip17xx_apply(struct switch_dev *dev)
  692. {
  693. struct ip17xx_state *state = get_state(dev);
  694. if (REG_SUPP(state->regs->MII_REGISTER_EN)) {
  695. int val = getPhy(state, state->regs->MII_REGISTER_EN);
  696. if (val < 0) {
  697. return val;
  698. }
  699. val |= (1<<state->regs->MII_REGISTER_EN_BIT);
  700. return setPhy(state, state->regs->MII_REGISTER_EN, val);
  701. }
  702. return 0;
  703. }
  704. static int ip17xx_reset(struct switch_dev *dev)
  705. {
  706. struct ip17xx_state *state = get_state(dev);
  707. int i, err;
  708. if (REG_SUPP(state->regs->RESET_REG)) {
  709. err = setPhy(state, state->regs->RESET_REG, state->regs->RESET_VAL);
  710. if (err < 0)
  711. return err;
  712. err = getPhy(state, state->regs->RESET_REG);
  713. /*
  714. * Data sheet specifies reset period to be 2 msec.
  715. * (I don't see any mention of the 2ms delay in the IP178C spec, only
  716. * in IP175C, but it can't hurt.)
  717. */
  718. mdelay(2);
  719. }
  720. /* reset switch ports */
  721. for (i = 0; i < state->regs->NUM_PORTS-1; i++) {
  722. err = ip_phy_write(state, i, MII_BMCR, BMCR_RESET);
  723. if (err < 0)
  724. return err;
  725. }
  726. state->router_mode = 0;
  727. state->vlan_enabled = 0;
  728. ip17xx_reset_vlan_config(state);
  729. return state->regs->reset(state);
  730. }
  731. static int ip17xx_get_tagged(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  732. {
  733. struct ip17xx_state *state = get_state(dev);
  734. if (state->add_tag & (1<<val->port_vlan)) {
  735. if (state->remove_tag & (1<<val->port_vlan))
  736. val->value.i = 3; // shouldn't ever happen.
  737. else
  738. val->value.i = 1;
  739. } else {
  740. if (state->remove_tag & (1<<val->port_vlan))
  741. val->value.i = 0;
  742. else
  743. val->value.i = 2;
  744. }
  745. return 0;
  746. }
  747. static int ip17xx_set_tagged(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  748. {
  749. struct ip17xx_state *state = get_state(dev);
  750. state->add_tag &= ~(1<<val->port_vlan);
  751. state->remove_tag &= ~(1<<val->port_vlan);
  752. if (val->value.i == 0)
  753. state->remove_tag |= (1<<val->port_vlan);
  754. if (val->value.i == 1)
  755. state->add_tag |= (1<<val->port_vlan);
  756. return state->regs->update_state(state);
  757. }
  758. /** Get the current phy address */
  759. static int ip17xx_get_phy(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  760. {
  761. struct ip17xx_state *state = get_state(dev);
  762. val->value.i = state->proc_mii.p;
  763. return 0;
  764. }
  765. /** Set a new phy address for low level access to registers */
  766. static int ip17xx_set_phy(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  767. {
  768. struct ip17xx_state *state = get_state(dev);
  769. int new_reg = val->value.i;
  770. if (new_reg < 0 || new_reg > 31)
  771. state->proc_mii.p = (u16)-1;
  772. else
  773. state->proc_mii.p = (u16)new_reg;
  774. return 0;
  775. }
  776. /** Get the current register number */
  777. static int ip17xx_get_reg(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  778. {
  779. struct ip17xx_state *state = get_state(dev);
  780. val->value.i = state->proc_mii.m;
  781. return 0;
  782. }
  783. /** Set a new register address for low level access to registers */
  784. static int ip17xx_set_reg(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  785. {
  786. struct ip17xx_state *state = get_state(dev);
  787. int new_reg = val->value.i;
  788. if (new_reg < 0 || new_reg > 31)
  789. state->proc_mii.m = (u16)-1;
  790. else
  791. state->proc_mii.m = (u16)new_reg;
  792. return 0;
  793. }
  794. /** Get the register content of state->proc_mii */
  795. static int ip17xx_get_val(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  796. {
  797. struct ip17xx_state *state = get_state(dev);
  798. int retval = -EINVAL;
  799. if (REG_SUPP(state->proc_mii))
  800. retval = getPhy(state, state->proc_mii);
  801. if (retval < 0) {
  802. return retval;
  803. } else {
  804. val->value.i = retval;
  805. return 0;
  806. }
  807. }
  808. /** Write a value to the register defined by phy/reg above */
  809. static int ip17xx_set_val(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  810. {
  811. struct ip17xx_state *state = get_state(dev);
  812. int myval, err = -EINVAL;
  813. myval = val->value.i;
  814. if (myval <= 0xffff && myval >= 0 && REG_SUPP(state->proc_mii)) {
  815. err = setPhy(state, state->proc_mii, (u16)myval);
  816. }
  817. return err;
  818. }
  819. static int ip17xx_read_name(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  820. {
  821. struct ip17xx_state *state = get_state(dev);
  822. val->value.s = state->regs->NAME; // Just a const pointer, won't be freed by swconfig.
  823. return 0;
  824. }
  825. static int ip17xx_get_tag(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  826. {
  827. struct ip17xx_state *state = get_state(dev);
  828. int vlan = val->port_vlan;
  829. if (vlan < 0 || vlan >= MAX_VLANS)
  830. return -EINVAL;
  831. val->value.i = state->vlans[vlan].tag;
  832. return 0;
  833. }
  834. static int ip17xx_set_tag(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  835. {
  836. struct ip17xx_state *state = get_state(dev);
  837. int vlan = val->port_vlan;
  838. int tag = val->value.i;
  839. if (vlan < 0 || vlan >= MAX_VLANS)
  840. return -EINVAL;
  841. if (tag < 0 || tag > 4095)
  842. return -EINVAL;
  843. state->vlans[vlan].tag = tag;
  844. return state->regs->update_state(state);
  845. }
  846. static int ip17xx_set_port_speed(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  847. {
  848. struct ip17xx_state *state = get_state(dev);
  849. int nr = val->port_vlan;
  850. int ctrl;
  851. int autoneg;
  852. int speed;
  853. if (val->value.i == 100) {
  854. speed = 1;
  855. autoneg = 0;
  856. } else if (val->value.i == 10) {
  857. speed = 0;
  858. autoneg = 0;
  859. } else {
  860. autoneg = 1;
  861. speed = 1;
  862. }
  863. /* Can't set speed for cpu port */
  864. if (nr == state->regs->CPU_PORT)
  865. return -EINVAL;
  866. if (nr >= dev->ports || nr < 0)
  867. return -EINVAL;
  868. ctrl = ip_phy_read(state, nr, 0);
  869. if (ctrl < 0)
  870. return -EIO;
  871. ctrl &= (~(1<<12));
  872. ctrl &= (~(1<<13));
  873. ctrl |= (autoneg<<12);
  874. ctrl |= (speed<<13);
  875. return ip_phy_write(state, nr, 0, ctrl);
  876. }
  877. static int ip17xx_get_port_speed(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  878. {
  879. struct ip17xx_state *state = get_state(dev);
  880. int nr = val->port_vlan;
  881. int speed, status;
  882. if (nr == state->regs->CPU_PORT) {
  883. val->value.i = 100;
  884. return 0;
  885. }
  886. if (nr >= dev->ports || nr < 0)
  887. return -EINVAL;
  888. status = ip_phy_read(state, nr, 1);
  889. speed = ip_phy_read(state, nr, 18);
  890. if (status < 0 || speed < 0)
  891. return -EIO;
  892. if (status & 4)
  893. val->value.i = ((speed & (1<<11)) ? 100 : 10);
  894. else
  895. val->value.i = 0;
  896. return 0;
  897. }
  898. static int ip17xx_get_port_status(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
  899. {
  900. struct ip17xx_state *state = get_state(dev);
  901. int ctrl, speed, status;
  902. int nr = val->port_vlan;
  903. int len;
  904. char *buf = state->buf; // fixed-length at 80.
  905. if (nr == state->regs->CPU_PORT) {
  906. sprintf(buf, "up, 100 Mbps, cpu port");
  907. val->value.s = buf;
  908. return 0;
  909. }
  910. if (nr >= dev->ports || nr < 0)
  911. return -EINVAL;
  912. ctrl = ip_phy_read(state, nr, 0);
  913. status = ip_phy_read(state, nr, 1);
  914. speed = ip_phy_read(state, nr, 18);
  915. if (ctrl < 0 || status < 0 || speed < 0)
  916. return -EIO;
  917. if (status & 4)
  918. len = sprintf(buf, "up, %d Mbps, %s duplex",
  919. ((speed & (1<<11)) ? 100 : 10),
  920. ((speed & (1<<10)) ? "full" : "half"));
  921. else
  922. len = sprintf(buf, "down");
  923. if (ctrl & (1<<12)) {
  924. len += sprintf(buf+len, ", auto-negotiate");
  925. if (!(status & (1<<5)))
  926. len += sprintf(buf+len, " (in progress)");
  927. } else {
  928. len += sprintf(buf+len, ", fixed speed (%d)",
  929. ((ctrl & (1<<13)) ? 100 : 10));
  930. }
  931. buf[len] = '\0';
  932. val->value.s = buf;
  933. return 0;
  934. }
  935. static int ip17xx_get_pvid(struct switch_dev *dev, int port, int *val)
  936. {
  937. struct ip17xx_state *state = get_state(dev);
  938. *val = state->ports[port].pvid;
  939. return 0;
  940. }
  941. static int ip17xx_set_pvid(struct switch_dev *dev, int port, int val)
  942. {
  943. struct ip17xx_state *state = get_state(dev);
  944. if (val < 0 || val >= MAX_VLANS)
  945. return -EINVAL;
  946. state->ports[port].pvid = val;
  947. return state->regs->update_state(state);
  948. }
  949. enum Ports {
  950. IP17XX_PORT_STATUS,
  951. IP17XX_PORT_LINK,
  952. IP17XX_PORT_TAGGED,
  953. IP17XX_PORT_PVID,
  954. };
  955. enum Globals {
  956. IP17XX_ENABLE_VLAN,
  957. IP17XX_GET_NAME,
  958. IP17XX_REGISTER_PHY,
  959. IP17XX_REGISTER_MII,
  960. IP17XX_REGISTER_VALUE,
  961. IP17XX_REGISTER_ERRNO,
  962. };
  963. enum Vlans {
  964. IP17XX_VLAN_TAG,
  965. };
  966. static const struct switch_attr ip17xx_global[] = {
  967. [IP17XX_ENABLE_VLAN] = {
  968. .id = IP17XX_ENABLE_VLAN,
  969. .type = SWITCH_TYPE_INT,
  970. .name = "enable_vlan",
  971. .description = "Flag to enable or disable VLANs and tagging",
  972. .get = ip17xx_get_enable_vlan,
  973. .set = ip17xx_set_enable_vlan,
  974. },
  975. [IP17XX_GET_NAME] = {
  976. .id = IP17XX_GET_NAME,
  977. .type = SWITCH_TYPE_STRING,
  978. .description = "Returns the type of IC+ chip.",
  979. .name = "name",
  980. .get = ip17xx_read_name,
  981. .set = NULL,
  982. },
  983. /* jal: added for low level debugging etc. */
  984. [IP17XX_REGISTER_PHY] = {
  985. .id = IP17XX_REGISTER_PHY,
  986. .type = SWITCH_TYPE_INT,
  987. .description = "Direct register access: set PHY (0-4, or 29,30,31)",
  988. .name = "phy",
  989. .get = ip17xx_get_phy,
  990. .set = ip17xx_set_phy,
  991. },
  992. [IP17XX_REGISTER_MII] = {
  993. .id = IP17XX_REGISTER_MII,
  994. .type = SWITCH_TYPE_INT,
  995. .description = "Direct register access: set MII register number (0-31)",
  996. .name = "reg",
  997. .get = ip17xx_get_reg,
  998. .set = ip17xx_set_reg,
  999. },
  1000. [IP17XX_REGISTER_VALUE] = {
  1001. .id = IP17XX_REGISTER_VALUE,
  1002. .type = SWITCH_TYPE_INT,
  1003. .description = "Direct register access: read/write to register (0-65535)",
  1004. .name = "val",
  1005. .get = ip17xx_get_val,
  1006. .set = ip17xx_set_val,
  1007. },
  1008. };
  1009. static const struct switch_attr ip17xx_vlan[] = {
  1010. [IP17XX_VLAN_TAG] = {
  1011. .id = IP17XX_VLAN_TAG,
  1012. .type = SWITCH_TYPE_INT,
  1013. .description = "VLAN ID (0-4095) [IP175D only]",
  1014. .name = "vid",
  1015. .get = ip17xx_get_tag,
  1016. .set = ip17xx_set_tag,
  1017. }
  1018. };
  1019. static const struct switch_attr ip17xx_port[] = {
  1020. [IP17XX_PORT_STATUS] = {
  1021. .id = IP17XX_PORT_STATUS,
  1022. .type = SWITCH_TYPE_STRING,
  1023. .description = "Returns Detailed port status",
  1024. .name = "status",
  1025. .get = ip17xx_get_port_status,
  1026. .set = NULL,
  1027. },
  1028. [IP17XX_PORT_LINK] = {
  1029. .id = IP17XX_PORT_LINK,
  1030. .type = SWITCH_TYPE_INT,
  1031. .description = "Link speed. Can write 0 for auto-negotiate, or 10 or 100",
  1032. .name = "link",
  1033. .get = ip17xx_get_port_speed,
  1034. .set = ip17xx_set_port_speed,
  1035. },
  1036. [IP17XX_PORT_TAGGED] = {
  1037. .id = IP17XX_PORT_LINK,
  1038. .type = SWITCH_TYPE_INT,
  1039. .description = "0 = untag, 1 = add tags, 2 = do not alter (This value is reset if vlans are altered)",
  1040. .name = "tagged",
  1041. .get = ip17xx_get_tagged,
  1042. .set = ip17xx_set_tagged,
  1043. },
  1044. };
  1045. static const struct switch_dev_ops ip17xx_ops = {
  1046. .attr_global = {
  1047. .attr = ip17xx_global,
  1048. .n_attr = ARRAY_SIZE(ip17xx_global),
  1049. },
  1050. .attr_port = {
  1051. .attr = ip17xx_port,
  1052. .n_attr = ARRAY_SIZE(ip17xx_port),
  1053. },
  1054. .attr_vlan = {
  1055. .attr = ip17xx_vlan,
  1056. .n_attr = ARRAY_SIZE(ip17xx_vlan),
  1057. },
  1058. .get_port_pvid = ip17xx_get_pvid,
  1059. .set_port_pvid = ip17xx_set_pvid,
  1060. .get_vlan_ports = ip17xx_get_ports,
  1061. .set_vlan_ports = ip17xx_set_ports,
  1062. .apply_config = ip17xx_apply,
  1063. .reset_switch = ip17xx_reset,
  1064. };
  1065. static int ip17xx_probe(struct phy_device *pdev)
  1066. {
  1067. struct ip17xx_state *state;
  1068. struct switch_dev *dev;
  1069. int err;
  1070. /* We only attach to PHY 0, but use all available PHYs */
  1071. if (pdev->addr != 0)
  1072. return -ENODEV;
  1073. state = kzalloc(sizeof(*state), GFP_KERNEL);
  1074. if (!state)
  1075. return -ENOMEM;
  1076. dev = &state->dev;
  1077. pdev->priv = state;
  1078. state->mii_bus = pdev->bus;
  1079. err = get_model(state);
  1080. if (err < 0)
  1081. goto error;
  1082. dev->vlans = MAX_VLANS;
  1083. dev->cpu_port = state->regs->CPU_PORT;
  1084. dev->ports = state->regs->NUM_PORTS;
  1085. dev->name = state->regs->NAME;
  1086. dev->ops = &ip17xx_ops;
  1087. pr_info("IP17xx: Found %s at %s\n", dev->name, dev_name(&pdev->dev));
  1088. return 0;
  1089. error:
  1090. kfree(state);
  1091. return err;
  1092. }
  1093. static int ip17xx_config_init(struct phy_device *pdev)
  1094. {
  1095. struct ip17xx_state *state = pdev->priv;
  1096. struct net_device *dev = pdev->attached_dev;
  1097. int err;
  1098. err = register_switch(&state->dev, dev);
  1099. if (err < 0)
  1100. return err;
  1101. state->registered = true;
  1102. ip17xx_reset(&state->dev);
  1103. return 0;
  1104. }
  1105. static void ip17xx_remove(struct phy_device *pdev)
  1106. {
  1107. struct ip17xx_state *state = pdev->priv;
  1108. if (state->registered)
  1109. unregister_switch(&state->dev);
  1110. kfree(state);
  1111. }
  1112. static int ip17xx_config_aneg(struct phy_device *pdev)
  1113. {
  1114. return 0;
  1115. }
  1116. static int ip17xx_aneg_done(struct phy_device *pdev)
  1117. {
  1118. return BMSR_ANEGCOMPLETE;
  1119. }
  1120. static int ip17xx_update_link(struct phy_device *pdev)
  1121. {
  1122. pdev->link = 1;
  1123. return 0;
  1124. }
  1125. static int ip17xx_read_status(struct phy_device *pdev)
  1126. {
  1127. pdev->speed = SPEED_100;
  1128. pdev->duplex = DUPLEX_FULL;
  1129. pdev->pause = pdev->asym_pause = 0;
  1130. pdev->link = 1;
  1131. return 0;
  1132. }
  1133. static struct phy_driver ip17xx_driver = {
  1134. .name = "IC+ IP17xx",
  1135. .phy_id = 0x02430c00,
  1136. .phy_id_mask = 0x0ffffc00,
  1137. .features = PHY_BASIC_FEATURES,
  1138. .probe = ip17xx_probe,
  1139. .remove = ip17xx_remove,
  1140. .config_init = ip17xx_config_init,
  1141. .config_aneg = ip17xx_config_aneg,
  1142. .aneg_done = ip17xx_aneg_done,
  1143. .update_link = ip17xx_update_link,
  1144. .read_status = ip17xx_read_status,
  1145. .driver = { .owner = THIS_MODULE },
  1146. };
  1147. static struct phy_driver ip175a_driver = {
  1148. .name = "IC+ IP175A",
  1149. .phy_id = 0x02430c50,
  1150. .phy_id_mask = 0x0ffffff0,
  1151. .features = PHY_BASIC_FEATURES,
  1152. .probe = ip17xx_probe,
  1153. .remove = ip17xx_remove,
  1154. .config_init = ip17xx_config_init,
  1155. .config_aneg = ip17xx_config_aneg,
  1156. .aneg_done = ip17xx_aneg_done,
  1157. .update_link = ip17xx_update_link,
  1158. .read_status = ip17xx_read_status,
  1159. .driver = { .owner = THIS_MODULE },
  1160. };
  1161. int __init ip17xx_init(void)
  1162. {
  1163. int ret;
  1164. ret = phy_driver_register(&ip175a_driver);
  1165. if (ret < 0)
  1166. return ret;
  1167. return phy_driver_register(&ip17xx_driver);
  1168. }
  1169. void __exit ip17xx_exit(void)
  1170. {
  1171. phy_driver_unregister(&ip17xx_driver);
  1172. phy_driver_unregister(&ip175a_driver);
  1173. }
  1174. MODULE_AUTHOR("Patrick Horn <patrick.horn@gmail.com>");
  1175. MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
  1176. MODULE_AUTHOR("Martin Mares <mj@ucw.cz>");
  1177. MODULE_LICENSE("GPL");
  1178. module_init(ip17xx_init);
  1179. module_exit(ip17xx_exit);