mdio-octeon.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2009-2012 Cavium, Inc.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_mdio.h>
  11. #include <linux/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/gfp.h>
  14. #include <linux/phy.h>
  15. #include <linux/io.h>
  16. #ifdef CONFIG_CAVIUM_OCTEON_SOC
  17. #include <asm/octeon/octeon.h>
  18. #endif
  19. #define DRV_VERSION "1.1"
  20. #define DRV_DESCRIPTION "Cavium Networks Octeon/ThunderX SMI/MDIO driver"
  21. #define SMI_CMD 0x0
  22. #define SMI_WR_DAT 0x8
  23. #define SMI_RD_DAT 0x10
  24. #define SMI_CLK 0x18
  25. #define SMI_EN 0x20
  26. #ifdef __BIG_ENDIAN_BITFIELD
  27. #define OCT_MDIO_BITFIELD_FIELD(field, more) \
  28. field; \
  29. more
  30. #else
  31. #define OCT_MDIO_BITFIELD_FIELD(field, more) \
  32. more \
  33. field;
  34. #endif
  35. union cvmx_smix_clk {
  36. u64 u64;
  37. struct cvmx_smix_clk_s {
  38. OCT_MDIO_BITFIELD_FIELD(u64 reserved_25_63:39,
  39. OCT_MDIO_BITFIELD_FIELD(u64 mode:1,
  40. OCT_MDIO_BITFIELD_FIELD(u64 reserved_21_23:3,
  41. OCT_MDIO_BITFIELD_FIELD(u64 sample_hi:5,
  42. OCT_MDIO_BITFIELD_FIELD(u64 sample_mode:1,
  43. OCT_MDIO_BITFIELD_FIELD(u64 reserved_14_14:1,
  44. OCT_MDIO_BITFIELD_FIELD(u64 clk_idle:1,
  45. OCT_MDIO_BITFIELD_FIELD(u64 preamble:1,
  46. OCT_MDIO_BITFIELD_FIELD(u64 sample:4,
  47. OCT_MDIO_BITFIELD_FIELD(u64 phase:8,
  48. ;))))))))))
  49. } s;
  50. };
  51. union cvmx_smix_cmd {
  52. u64 u64;
  53. struct cvmx_smix_cmd_s {
  54. OCT_MDIO_BITFIELD_FIELD(u64 reserved_18_63:46,
  55. OCT_MDIO_BITFIELD_FIELD(u64 phy_op:2,
  56. OCT_MDIO_BITFIELD_FIELD(u64 reserved_13_15:3,
  57. OCT_MDIO_BITFIELD_FIELD(u64 phy_adr:5,
  58. OCT_MDIO_BITFIELD_FIELD(u64 reserved_5_7:3,
  59. OCT_MDIO_BITFIELD_FIELD(u64 reg_adr:5,
  60. ;))))))
  61. } s;
  62. };
  63. union cvmx_smix_en {
  64. u64 u64;
  65. struct cvmx_smix_en_s {
  66. OCT_MDIO_BITFIELD_FIELD(u64 reserved_1_63:63,
  67. OCT_MDIO_BITFIELD_FIELD(u64 en:1,
  68. ;))
  69. } s;
  70. };
  71. union cvmx_smix_rd_dat {
  72. u64 u64;
  73. struct cvmx_smix_rd_dat_s {
  74. OCT_MDIO_BITFIELD_FIELD(u64 reserved_18_63:46,
  75. OCT_MDIO_BITFIELD_FIELD(u64 pending:1,
  76. OCT_MDIO_BITFIELD_FIELD(u64 val:1,
  77. OCT_MDIO_BITFIELD_FIELD(u64 dat:16,
  78. ;))))
  79. } s;
  80. };
  81. union cvmx_smix_wr_dat {
  82. u64 u64;
  83. struct cvmx_smix_wr_dat_s {
  84. OCT_MDIO_BITFIELD_FIELD(u64 reserved_18_63:46,
  85. OCT_MDIO_BITFIELD_FIELD(u64 pending:1,
  86. OCT_MDIO_BITFIELD_FIELD(u64 val:1,
  87. OCT_MDIO_BITFIELD_FIELD(u64 dat:16,
  88. ;))))
  89. } s;
  90. };
  91. enum octeon_mdiobus_mode {
  92. UNINIT = 0,
  93. C22,
  94. C45
  95. };
  96. struct octeon_mdiobus {
  97. struct mii_bus *mii_bus;
  98. u64 register_base;
  99. resource_size_t mdio_phys;
  100. resource_size_t regsize;
  101. enum octeon_mdiobus_mode mode;
  102. int phy_irq[PHY_MAX_ADDR];
  103. };
  104. #ifdef CONFIG_CAVIUM_OCTEON_SOC
  105. static void oct_mdio_writeq(u64 val, u64 addr)
  106. {
  107. cvmx_write_csr(addr, val);
  108. }
  109. static u64 oct_mdio_readq(u64 addr)
  110. {
  111. return cvmx_read_csr(addr);
  112. }
  113. #else
  114. #define oct_mdio_writeq(val, addr) writeq_relaxed(val, (void *)addr)
  115. #define oct_mdio_readq(addr) readq_relaxed((void *)addr)
  116. #endif
  117. static void octeon_mdiobus_set_mode(struct octeon_mdiobus *p,
  118. enum octeon_mdiobus_mode m)
  119. {
  120. union cvmx_smix_clk smi_clk;
  121. if (m == p->mode)
  122. return;
  123. smi_clk.u64 = oct_mdio_readq(p->register_base + SMI_CLK);
  124. smi_clk.s.mode = (m == C45) ? 1 : 0;
  125. smi_clk.s.preamble = 1;
  126. oct_mdio_writeq(smi_clk.u64, p->register_base + SMI_CLK);
  127. p->mode = m;
  128. }
  129. static int octeon_mdiobus_c45_addr(struct octeon_mdiobus *p,
  130. int phy_id, int regnum)
  131. {
  132. union cvmx_smix_cmd smi_cmd;
  133. union cvmx_smix_wr_dat smi_wr;
  134. int timeout = 1000;
  135. octeon_mdiobus_set_mode(p, C45);
  136. smi_wr.u64 = 0;
  137. smi_wr.s.dat = regnum & 0xffff;
  138. oct_mdio_writeq(smi_wr.u64, p->register_base + SMI_WR_DAT);
  139. regnum = (regnum >> 16) & 0x1f;
  140. smi_cmd.u64 = 0;
  141. smi_cmd.s.phy_op = 0; /* MDIO_CLAUSE_45_ADDRESS */
  142. smi_cmd.s.phy_adr = phy_id;
  143. smi_cmd.s.reg_adr = regnum;
  144. oct_mdio_writeq(smi_cmd.u64, p->register_base + SMI_CMD);
  145. do {
  146. /* Wait 1000 clocks so we don't saturate the RSL bus
  147. * doing reads.
  148. */
  149. __delay(1000);
  150. smi_wr.u64 = oct_mdio_readq(p->register_base + SMI_WR_DAT);
  151. } while (smi_wr.s.pending && --timeout);
  152. if (timeout <= 0)
  153. return -EIO;
  154. return 0;
  155. }
  156. static int octeon_mdiobus_read(struct mii_bus *bus, int phy_id, int regnum)
  157. {
  158. struct octeon_mdiobus *p = bus->priv;
  159. union cvmx_smix_cmd smi_cmd;
  160. union cvmx_smix_rd_dat smi_rd;
  161. unsigned int op = 1; /* MDIO_CLAUSE_22_READ */
  162. int timeout = 1000;
  163. if (regnum & MII_ADDR_C45) {
  164. int r = octeon_mdiobus_c45_addr(p, phy_id, regnum);
  165. if (r < 0)
  166. return r;
  167. regnum = (regnum >> 16) & 0x1f;
  168. op = 3; /* MDIO_CLAUSE_45_READ */
  169. } else {
  170. octeon_mdiobus_set_mode(p, C22);
  171. }
  172. smi_cmd.u64 = 0;
  173. smi_cmd.s.phy_op = op;
  174. smi_cmd.s.phy_adr = phy_id;
  175. smi_cmd.s.reg_adr = regnum;
  176. oct_mdio_writeq(smi_cmd.u64, p->register_base + SMI_CMD);
  177. do {
  178. /* Wait 1000 clocks so we don't saturate the RSL bus
  179. * doing reads.
  180. */
  181. __delay(1000);
  182. smi_rd.u64 = oct_mdio_readq(p->register_base + SMI_RD_DAT);
  183. } while (smi_rd.s.pending && --timeout);
  184. if (smi_rd.s.val)
  185. return smi_rd.s.dat;
  186. else
  187. return -EIO;
  188. }
  189. static int octeon_mdiobus_write(struct mii_bus *bus, int phy_id,
  190. int regnum, u16 val)
  191. {
  192. struct octeon_mdiobus *p = bus->priv;
  193. union cvmx_smix_cmd smi_cmd;
  194. union cvmx_smix_wr_dat smi_wr;
  195. unsigned int op = 0; /* MDIO_CLAUSE_22_WRITE */
  196. int timeout = 1000;
  197. if (regnum & MII_ADDR_C45) {
  198. int r = octeon_mdiobus_c45_addr(p, phy_id, regnum);
  199. if (r < 0)
  200. return r;
  201. regnum = (regnum >> 16) & 0x1f;
  202. op = 1; /* MDIO_CLAUSE_45_WRITE */
  203. } else {
  204. octeon_mdiobus_set_mode(p, C22);
  205. }
  206. smi_wr.u64 = 0;
  207. smi_wr.s.dat = val;
  208. oct_mdio_writeq(smi_wr.u64, p->register_base + SMI_WR_DAT);
  209. smi_cmd.u64 = 0;
  210. smi_cmd.s.phy_op = op;
  211. smi_cmd.s.phy_adr = phy_id;
  212. smi_cmd.s.reg_adr = regnum;
  213. oct_mdio_writeq(smi_cmd.u64, p->register_base + SMI_CMD);
  214. do {
  215. /* Wait 1000 clocks so we don't saturate the RSL bus
  216. * doing reads.
  217. */
  218. __delay(1000);
  219. smi_wr.u64 = oct_mdio_readq(p->register_base + SMI_WR_DAT);
  220. } while (smi_wr.s.pending && --timeout);
  221. if (timeout <= 0)
  222. return -EIO;
  223. return 0;
  224. }
  225. static int octeon_mdiobus_probe(struct platform_device *pdev)
  226. {
  227. struct octeon_mdiobus *bus;
  228. struct resource *res_mem;
  229. union cvmx_smix_en smi_en;
  230. int err = -ENOENT;
  231. bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL);
  232. if (!bus)
  233. return -ENOMEM;
  234. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. if (res_mem == NULL) {
  236. dev_err(&pdev->dev, "found no memory resource\n");
  237. return -ENXIO;
  238. }
  239. bus->mdio_phys = res_mem->start;
  240. bus->regsize = resource_size(res_mem);
  241. if (!devm_request_mem_region(&pdev->dev, bus->mdio_phys, bus->regsize,
  242. res_mem->name)) {
  243. dev_err(&pdev->dev, "request_mem_region failed\n");
  244. return -ENXIO;
  245. }
  246. bus->register_base =
  247. (u64)devm_ioremap(&pdev->dev, bus->mdio_phys, bus->regsize);
  248. if (!bus->register_base) {
  249. dev_err(&pdev->dev, "dev_ioremap failed\n");
  250. return -ENOMEM;
  251. }
  252. bus->mii_bus = mdiobus_alloc();
  253. if (!bus->mii_bus)
  254. goto fail;
  255. smi_en.u64 = 0;
  256. smi_en.s.en = 1;
  257. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  258. bus->mii_bus->priv = bus;
  259. bus->mii_bus->irq = bus->phy_irq;
  260. bus->mii_bus->name = "mdio-octeon";
  261. snprintf(bus->mii_bus->id, MII_BUS_ID_SIZE, "%llx", bus->register_base);
  262. bus->mii_bus->parent = &pdev->dev;
  263. bus->mii_bus->read = octeon_mdiobus_read;
  264. bus->mii_bus->write = octeon_mdiobus_write;
  265. platform_set_drvdata(pdev, bus);
  266. err = of_mdiobus_register(bus->mii_bus, pdev->dev.of_node);
  267. if (err)
  268. goto fail_register;
  269. dev_info(&pdev->dev, "Version " DRV_VERSION "\n");
  270. return 0;
  271. fail_register:
  272. mdiobus_free(bus->mii_bus);
  273. fail:
  274. smi_en.u64 = 0;
  275. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  276. return err;
  277. }
  278. static int octeon_mdiobus_remove(struct platform_device *pdev)
  279. {
  280. struct octeon_mdiobus *bus;
  281. union cvmx_smix_en smi_en;
  282. bus = platform_get_drvdata(pdev);
  283. mdiobus_unregister(bus->mii_bus);
  284. mdiobus_free(bus->mii_bus);
  285. smi_en.u64 = 0;
  286. oct_mdio_writeq(smi_en.u64, bus->register_base + SMI_EN);
  287. return 0;
  288. }
  289. static const struct of_device_id octeon_mdiobus_match[] = {
  290. {
  291. .compatible = "cavium,octeon-3860-mdio",
  292. },
  293. {},
  294. };
  295. MODULE_DEVICE_TABLE(of, octeon_mdiobus_match);
  296. static struct platform_driver octeon_mdiobus_driver = {
  297. .driver = {
  298. .name = "mdio-octeon",
  299. .of_match_table = octeon_mdiobus_match,
  300. },
  301. .probe = octeon_mdiobus_probe,
  302. .remove = octeon_mdiobus_remove,
  303. };
  304. void octeon_mdiobus_force_mod_depencency(void)
  305. {
  306. /* Let ethernet drivers force us to be loaded. */
  307. }
  308. EXPORT_SYMBOL(octeon_mdiobus_force_mod_depencency);
  309. module_platform_driver(octeon_mdiobus_driver);
  310. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  311. MODULE_VERSION(DRV_VERSION);
  312. MODULE_AUTHOR("David Daney");
  313. MODULE_LICENSE("GPL");