davinci_mdio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DaVinci MDIO Module driver
  4. *
  5. * Copyright (C) 2010 Texas Instruments.
  6. *
  7. * Shamelessly ripped out of davinci_emac.c, original copyrights follow:
  8. *
  9. * Copyright (C) 2009 Texas Instruments.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/phy.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/iopoll.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/davinci_emac.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_mdio.h>
  28. #include <linux/pinctrl/consumer.h>
  29. #include <linux/mdio-bitbang.h>
  30. #include <linux/sys_soc.h>
  31. /*
  32. * This timeout definition is a worst-case ultra defensive measure against
  33. * unexpected controller lock ups. Ideally, we should never ever hit this
  34. * scenario in practice.
  35. */
  36. #define MDIO_TIMEOUT 100 /* msecs */
  37. #define PHY_REG_MASK 0x1f
  38. #define PHY_ID_MASK 0x1f
  39. #define DEF_OUT_FREQ 2200000 /* 2.2 MHz */
  40. struct davinci_mdio_of_param {
  41. int autosuspend_delay_ms;
  42. bool manual_mode;
  43. };
  44. struct davinci_mdio_regs {
  45. u32 version;
  46. u32 control;
  47. #define CONTROL_IDLE BIT(31)
  48. #define CONTROL_ENABLE BIT(30)
  49. #define CONTROL_MAX_DIV (0xffff)
  50. #define CONTROL_CLKDIV GENMASK(15, 0)
  51. #define MDIO_MAN_MDCLK_O BIT(2)
  52. #define MDIO_MAN_OE BIT(1)
  53. #define MDIO_MAN_PIN BIT(0)
  54. #define MDIO_MANUALMODE BIT(31)
  55. #define MDIO_PIN 0
  56. u32 alive;
  57. u32 link;
  58. u32 linkintraw;
  59. u32 linkintmasked;
  60. u32 __reserved_0[2];
  61. u32 userintraw;
  62. u32 userintmasked;
  63. u32 userintmaskset;
  64. u32 userintmaskclr;
  65. u32 manualif;
  66. u32 poll;
  67. u32 __reserved_1[18];
  68. struct {
  69. u32 access;
  70. #define USERACCESS_GO BIT(31)
  71. #define USERACCESS_WRITE BIT(30)
  72. #define USERACCESS_ACK BIT(29)
  73. #define USERACCESS_READ (0)
  74. #define USERACCESS_DATA (0xffff)
  75. u32 physel;
  76. } user[];
  77. };
  78. static const struct mdio_platform_data default_pdata = {
  79. .bus_freq = DEF_OUT_FREQ,
  80. };
  81. struct davinci_mdio_data {
  82. struct mdio_platform_data pdata;
  83. struct mdiobb_ctrl bb_ctrl;
  84. struct davinci_mdio_regs __iomem *regs;
  85. struct clk *clk;
  86. struct device *dev;
  87. struct mii_bus *bus;
  88. bool active_in_suspend;
  89. unsigned long access_time; /* jiffies */
  90. /* Indicates that driver shouldn't modify phy_mask in case
  91. * if MDIO bus is registered from DT.
  92. */
  93. bool skip_scan;
  94. u32 clk_div;
  95. bool manual_mode;
  96. };
  97. static void davinci_mdio_init_clk(struct davinci_mdio_data *data)
  98. {
  99. u32 mdio_in, div, mdio_out_khz, access_time;
  100. mdio_in = clk_get_rate(data->clk);
  101. div = (mdio_in / data->pdata.bus_freq) - 1;
  102. if (div > CONTROL_MAX_DIV)
  103. div = CONTROL_MAX_DIV;
  104. data->clk_div = div;
  105. /*
  106. * One mdio transaction consists of:
  107. * 32 bits of preamble
  108. * 32 bits of transferred data
  109. * 24 bits of bus yield (not needed unless shared?)
  110. */
  111. mdio_out_khz = mdio_in / (1000 * (div + 1));
  112. access_time = (88 * 1000) / mdio_out_khz;
  113. /*
  114. * In the worst case, we could be kicking off a user-access immediately
  115. * after the mdio bus scan state-machine triggered its own read. If
  116. * so, our request could get deferred by one access cycle. We
  117. * defensively allow for 4 access cycles.
  118. */
  119. data->access_time = usecs_to_jiffies(access_time * 4);
  120. if (!data->access_time)
  121. data->access_time = 1;
  122. }
  123. static void davinci_mdio_enable(struct davinci_mdio_data *data)
  124. {
  125. /* set enable and clock divider */
  126. writel(data->clk_div | CONTROL_ENABLE, &data->regs->control);
  127. }
  128. static void davinci_mdio_disable(struct davinci_mdio_data *data)
  129. {
  130. u32 reg;
  131. /* Disable MDIO state machine */
  132. reg = readl(&data->regs->control);
  133. reg &= ~CONTROL_CLKDIV;
  134. reg |= data->clk_div;
  135. reg &= ~CONTROL_ENABLE;
  136. writel(reg, &data->regs->control);
  137. }
  138. static void davinci_mdio_enable_manual_mode(struct davinci_mdio_data *data)
  139. {
  140. u32 reg;
  141. /* set manual mode */
  142. reg = readl(&data->regs->poll);
  143. reg |= MDIO_MANUALMODE;
  144. writel(reg, &data->regs->poll);
  145. }
  146. static void davinci_set_mdc(struct mdiobb_ctrl *ctrl, int level)
  147. {
  148. struct davinci_mdio_data *data;
  149. u32 reg;
  150. data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
  151. reg = readl(&data->regs->manualif);
  152. if (level)
  153. reg |= MDIO_MAN_MDCLK_O;
  154. else
  155. reg &= ~MDIO_MAN_MDCLK_O;
  156. writel(reg, &data->regs->manualif);
  157. }
  158. static void davinci_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
  159. {
  160. struct davinci_mdio_data *data;
  161. u32 reg;
  162. data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
  163. reg = readl(&data->regs->manualif);
  164. if (output)
  165. reg |= MDIO_MAN_OE;
  166. else
  167. reg &= ~MDIO_MAN_OE;
  168. writel(reg, &data->regs->manualif);
  169. }
  170. static void davinci_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
  171. {
  172. struct davinci_mdio_data *data;
  173. u32 reg;
  174. data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
  175. reg = readl(&data->regs->manualif);
  176. if (value)
  177. reg |= MDIO_MAN_PIN;
  178. else
  179. reg &= ~MDIO_MAN_PIN;
  180. writel(reg, &data->regs->manualif);
  181. }
  182. static int davinci_get_mdio_data(struct mdiobb_ctrl *ctrl)
  183. {
  184. struct davinci_mdio_data *data;
  185. unsigned long reg;
  186. data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
  187. reg = readl(&data->regs->manualif);
  188. return test_bit(MDIO_PIN, &reg);
  189. }
  190. static int davinci_mdiobb_read(struct mii_bus *bus, int phy, int reg)
  191. {
  192. int ret;
  193. ret = pm_runtime_resume_and_get(bus->parent);
  194. if (ret < 0)
  195. return ret;
  196. ret = mdiobb_read(bus, phy, reg);
  197. pm_runtime_mark_last_busy(bus->parent);
  198. pm_runtime_put_autosuspend(bus->parent);
  199. return ret;
  200. }
  201. static int davinci_mdiobb_write(struct mii_bus *bus, int phy, int reg,
  202. u16 val)
  203. {
  204. int ret;
  205. ret = pm_runtime_resume_and_get(bus->parent);
  206. if (ret < 0)
  207. return ret;
  208. ret = mdiobb_write(bus, phy, reg, val);
  209. pm_runtime_mark_last_busy(bus->parent);
  210. pm_runtime_put_autosuspend(bus->parent);
  211. return ret;
  212. }
  213. static int davinci_mdio_common_reset(struct davinci_mdio_data *data)
  214. {
  215. u32 phy_mask, ver;
  216. int ret;
  217. ret = pm_runtime_resume_and_get(data->dev);
  218. if (ret < 0)
  219. return ret;
  220. if (data->manual_mode) {
  221. davinci_mdio_disable(data);
  222. davinci_mdio_enable_manual_mode(data);
  223. }
  224. /* wait for scan logic to settle */
  225. msleep(PHY_MAX_ADDR * data->access_time);
  226. /* dump hardware version info */
  227. ver = readl(&data->regs->version);
  228. dev_info(data->dev,
  229. "davinci mdio revision %d.%d, bus freq %ld\n",
  230. (ver >> 8) & 0xff, ver & 0xff,
  231. data->pdata.bus_freq);
  232. if (data->skip_scan)
  233. goto done;
  234. /* get phy mask from the alive register */
  235. phy_mask = readl(&data->regs->alive);
  236. if (phy_mask) {
  237. /* restrict mdio bus to live phys only */
  238. dev_info(data->dev, "detected phy mask %x\n", ~phy_mask);
  239. phy_mask = ~phy_mask;
  240. } else {
  241. /* desperately scan all phys */
  242. dev_warn(data->dev, "no live phy, scanning all\n");
  243. phy_mask = 0;
  244. }
  245. data->bus->phy_mask = phy_mask;
  246. done:
  247. pm_runtime_mark_last_busy(data->dev);
  248. pm_runtime_put_autosuspend(data->dev);
  249. return 0;
  250. }
  251. static int davinci_mdio_reset(struct mii_bus *bus)
  252. {
  253. struct davinci_mdio_data *data = bus->priv;
  254. return davinci_mdio_common_reset(data);
  255. }
  256. static int davinci_mdiobb_reset(struct mii_bus *bus)
  257. {
  258. struct mdiobb_ctrl *ctrl = bus->priv;
  259. struct davinci_mdio_data *data;
  260. data = container_of(ctrl, struct davinci_mdio_data, bb_ctrl);
  261. return davinci_mdio_common_reset(data);
  262. }
  263. /* wait until hardware is ready for another user access */
  264. static inline int wait_for_user_access(struct davinci_mdio_data *data)
  265. {
  266. struct davinci_mdio_regs __iomem *regs = data->regs;
  267. unsigned long timeout = jiffies + msecs_to_jiffies(MDIO_TIMEOUT);
  268. u32 reg;
  269. while (time_after(timeout, jiffies)) {
  270. reg = readl(&regs->user[0].access);
  271. if ((reg & USERACCESS_GO) == 0)
  272. return 0;
  273. reg = readl(&regs->control);
  274. if ((reg & CONTROL_IDLE) == 0) {
  275. usleep_range(100, 200);
  276. continue;
  277. }
  278. /*
  279. * An emac soft_reset may have clobbered the mdio controller's
  280. * state machine. We need to reset and retry the current
  281. * operation
  282. */
  283. dev_warn(data->dev, "resetting idled controller\n");
  284. davinci_mdio_enable(data);
  285. return -EAGAIN;
  286. }
  287. reg = readl(&regs->user[0].access);
  288. if ((reg & USERACCESS_GO) == 0)
  289. return 0;
  290. dev_err(data->dev, "timed out waiting for user access\n");
  291. return -ETIMEDOUT;
  292. }
  293. /* wait until hardware state machine is idle */
  294. static inline int wait_for_idle(struct davinci_mdio_data *data)
  295. {
  296. struct davinci_mdio_regs __iomem *regs = data->regs;
  297. u32 val, ret;
  298. ret = readl_poll_timeout(&regs->control, val, val & CONTROL_IDLE,
  299. 0, MDIO_TIMEOUT * 1000);
  300. if (ret)
  301. dev_err(data->dev, "timed out waiting for idle\n");
  302. return ret;
  303. }
  304. static int davinci_mdio_read(struct mii_bus *bus, int phy_id, int phy_reg)
  305. {
  306. struct davinci_mdio_data *data = bus->priv;
  307. u32 reg;
  308. int ret;
  309. if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
  310. return -EINVAL;
  311. ret = pm_runtime_resume_and_get(data->dev);
  312. if (ret < 0)
  313. return ret;
  314. reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
  315. (phy_id << 16));
  316. while (1) {
  317. ret = wait_for_user_access(data);
  318. if (ret == -EAGAIN)
  319. continue;
  320. if (ret < 0)
  321. break;
  322. writel(reg, &data->regs->user[0].access);
  323. ret = wait_for_user_access(data);
  324. if (ret == -EAGAIN)
  325. continue;
  326. if (ret < 0)
  327. break;
  328. reg = readl(&data->regs->user[0].access);
  329. ret = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -EIO;
  330. break;
  331. }
  332. pm_runtime_mark_last_busy(data->dev);
  333. pm_runtime_put_autosuspend(data->dev);
  334. return ret;
  335. }
  336. static int davinci_mdio_write(struct mii_bus *bus, int phy_id,
  337. int phy_reg, u16 phy_data)
  338. {
  339. struct davinci_mdio_data *data = bus->priv;
  340. u32 reg;
  341. int ret;
  342. if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
  343. return -EINVAL;
  344. ret = pm_runtime_resume_and_get(data->dev);
  345. if (ret < 0)
  346. return ret;
  347. reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
  348. (phy_id << 16) | (phy_data & USERACCESS_DATA));
  349. while (1) {
  350. ret = wait_for_user_access(data);
  351. if (ret == -EAGAIN)
  352. continue;
  353. if (ret < 0)
  354. break;
  355. writel(reg, &data->regs->user[0].access);
  356. ret = wait_for_user_access(data);
  357. if (ret == -EAGAIN)
  358. continue;
  359. break;
  360. }
  361. pm_runtime_mark_last_busy(data->dev);
  362. pm_runtime_put_autosuspend(data->dev);
  363. return ret;
  364. }
  365. static int davinci_mdio_probe_dt(struct mdio_platform_data *data,
  366. struct platform_device *pdev)
  367. {
  368. struct device_node *node = pdev->dev.of_node;
  369. u32 prop;
  370. if (!node)
  371. return -EINVAL;
  372. if (of_property_read_u32(node, "bus_freq", &prop)) {
  373. dev_err(&pdev->dev, "Missing bus_freq property in the DT.\n");
  374. return -EINVAL;
  375. }
  376. data->bus_freq = prop;
  377. return 0;
  378. }
  379. struct k3_mdio_soc_data {
  380. bool manual_mode;
  381. };
  382. static const struct k3_mdio_soc_data am65_mdio_soc_data = {
  383. .manual_mode = true,
  384. };
  385. static const struct soc_device_attribute k3_mdio_socinfo[] = {
  386. { .family = "AM62X", .revision = "SR1.0", .data = &am65_mdio_soc_data },
  387. { .family = "AM64X", .revision = "SR1.0", .data = &am65_mdio_soc_data },
  388. { .family = "AM64X", .revision = "SR2.0", .data = &am65_mdio_soc_data },
  389. { .family = "AM65X", .revision = "SR1.0", .data = &am65_mdio_soc_data },
  390. { .family = "AM65X", .revision = "SR2.0", .data = &am65_mdio_soc_data },
  391. { .family = "J7200", .revision = "SR1.0", .data = &am65_mdio_soc_data },
  392. { .family = "J7200", .revision = "SR2.0", .data = &am65_mdio_soc_data },
  393. { .family = "J721E", .revision = "SR1.0", .data = &am65_mdio_soc_data },
  394. { .family = "J721E", .revision = "SR2.0", .data = &am65_mdio_soc_data },
  395. { .family = "J721S2", .revision = "SR1.0", .data = &am65_mdio_soc_data},
  396. { /* sentinel */ },
  397. };
  398. #if IS_ENABLED(CONFIG_OF)
  399. static const struct davinci_mdio_of_param of_cpsw_mdio_data = {
  400. .autosuspend_delay_ms = 100,
  401. };
  402. static const struct of_device_id davinci_mdio_of_mtable[] = {
  403. { .compatible = "ti,davinci_mdio", },
  404. { .compatible = "ti,cpsw-mdio", .data = &of_cpsw_mdio_data},
  405. { /* sentinel */ },
  406. };
  407. MODULE_DEVICE_TABLE(of, davinci_mdio_of_mtable);
  408. #endif
  409. static const struct mdiobb_ops davinci_mdiobb_ops = {
  410. .owner = THIS_MODULE,
  411. .set_mdc = davinci_set_mdc,
  412. .set_mdio_dir = davinci_set_mdio_dir,
  413. .set_mdio_data = davinci_set_mdio_data,
  414. .get_mdio_data = davinci_get_mdio_data,
  415. };
  416. static int davinci_mdio_probe(struct platform_device *pdev)
  417. {
  418. struct mdio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  419. struct device *dev = &pdev->dev;
  420. struct davinci_mdio_data *data;
  421. struct resource *res;
  422. struct phy_device *phy;
  423. int ret, addr;
  424. int autosuspend_delay_ms = -1;
  425. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  426. if (!data)
  427. return -ENOMEM;
  428. data->manual_mode = false;
  429. data->bb_ctrl.ops = &davinci_mdiobb_ops;
  430. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  431. const struct soc_device_attribute *soc_match_data;
  432. soc_match_data = soc_device_match(k3_mdio_socinfo);
  433. if (soc_match_data && soc_match_data->data) {
  434. const struct k3_mdio_soc_data *socdata =
  435. soc_match_data->data;
  436. data->manual_mode = socdata->manual_mode;
  437. }
  438. }
  439. if (data->manual_mode)
  440. data->bus = alloc_mdio_bitbang(&data->bb_ctrl);
  441. else
  442. data->bus = devm_mdiobus_alloc(dev);
  443. if (!data->bus) {
  444. dev_err(dev, "failed to alloc mii bus\n");
  445. return -ENOMEM;
  446. }
  447. if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
  448. const struct davinci_mdio_of_param *of_mdio_data;
  449. ret = davinci_mdio_probe_dt(&data->pdata, pdev);
  450. if (ret)
  451. return ret;
  452. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
  453. of_mdio_data = of_device_get_match_data(&pdev->dev);
  454. if (of_mdio_data) {
  455. autosuspend_delay_ms =
  456. of_mdio_data->autosuspend_delay_ms;
  457. }
  458. } else {
  459. data->pdata = pdata ? (*pdata) : default_pdata;
  460. snprintf(data->bus->id, MII_BUS_ID_SIZE, "%s-%x",
  461. pdev->name, pdev->id);
  462. }
  463. data->bus->name = dev_name(dev);
  464. if (data->manual_mode) {
  465. data->bus->read = davinci_mdiobb_read;
  466. data->bus->write = davinci_mdiobb_write;
  467. data->bus->reset = davinci_mdiobb_reset;
  468. dev_info(dev, "Configuring MDIO in manual mode\n");
  469. } else {
  470. data->bus->read = davinci_mdio_read;
  471. data->bus->write = davinci_mdio_write;
  472. data->bus->reset = davinci_mdio_reset;
  473. data->bus->priv = data;
  474. }
  475. data->bus->parent = dev;
  476. data->clk = devm_clk_get(dev, "fck");
  477. if (IS_ERR(data->clk)) {
  478. dev_err(dev, "failed to get device clock\n");
  479. return PTR_ERR(data->clk);
  480. }
  481. dev_set_drvdata(dev, data);
  482. data->dev = dev;
  483. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  484. if (!res)
  485. return -EINVAL;
  486. data->regs = devm_ioremap(dev, res->start, resource_size(res));
  487. if (!data->regs)
  488. return -ENOMEM;
  489. davinci_mdio_init_clk(data);
  490. pm_runtime_set_autosuspend_delay(&pdev->dev, autosuspend_delay_ms);
  491. pm_runtime_use_autosuspend(&pdev->dev);
  492. pm_runtime_enable(&pdev->dev);
  493. /* register the mii bus
  494. * Create PHYs from DT only in case if PHY child nodes are explicitly
  495. * defined to support backward compatibility with DTs which assume that
  496. * Davinci MDIO will always scan the bus for PHYs detection.
  497. */
  498. if (dev->of_node && of_get_child_count(dev->of_node))
  499. data->skip_scan = true;
  500. ret = of_mdiobus_register(data->bus, dev->of_node);
  501. if (ret)
  502. goto bail_out;
  503. /* scan and dump the bus */
  504. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  505. phy = mdiobus_get_phy(data->bus, addr);
  506. if (phy) {
  507. dev_info(dev, "phy[%d]: device %s, driver %s\n",
  508. phy->mdio.addr, phydev_name(phy),
  509. phy->drv ? phy->drv->name : "unknown");
  510. }
  511. }
  512. return 0;
  513. bail_out:
  514. pm_runtime_dont_use_autosuspend(&pdev->dev);
  515. pm_runtime_disable(&pdev->dev);
  516. return ret;
  517. }
  518. static int davinci_mdio_remove(struct platform_device *pdev)
  519. {
  520. struct davinci_mdio_data *data = platform_get_drvdata(pdev);
  521. if (data->bus) {
  522. mdiobus_unregister(data->bus);
  523. if (data->manual_mode)
  524. free_mdio_bitbang(data->bus);
  525. }
  526. pm_runtime_dont_use_autosuspend(&pdev->dev);
  527. pm_runtime_disable(&pdev->dev);
  528. return 0;
  529. }
  530. #ifdef CONFIG_PM
  531. static int davinci_mdio_runtime_suspend(struct device *dev)
  532. {
  533. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  534. u32 ctrl;
  535. /* shutdown the scan state machine */
  536. ctrl = readl(&data->regs->control);
  537. ctrl &= ~CONTROL_ENABLE;
  538. writel(ctrl, &data->regs->control);
  539. if (!data->manual_mode)
  540. wait_for_idle(data);
  541. return 0;
  542. }
  543. static int davinci_mdio_runtime_resume(struct device *dev)
  544. {
  545. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  546. if (data->manual_mode) {
  547. davinci_mdio_disable(data);
  548. davinci_mdio_enable_manual_mode(data);
  549. } else {
  550. davinci_mdio_enable(data);
  551. }
  552. return 0;
  553. }
  554. #endif
  555. #ifdef CONFIG_PM_SLEEP
  556. static int davinci_mdio_suspend(struct device *dev)
  557. {
  558. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  559. int ret = 0;
  560. data->active_in_suspend = !pm_runtime_status_suspended(dev);
  561. if (data->active_in_suspend)
  562. ret = pm_runtime_force_suspend(dev);
  563. if (ret < 0)
  564. return ret;
  565. /* Select sleep pin state */
  566. pinctrl_pm_select_sleep_state(dev);
  567. return 0;
  568. }
  569. static int davinci_mdio_resume(struct device *dev)
  570. {
  571. struct davinci_mdio_data *data = dev_get_drvdata(dev);
  572. /* Select default pin state */
  573. pinctrl_pm_select_default_state(dev);
  574. if (data->active_in_suspend)
  575. pm_runtime_force_resume(dev);
  576. return 0;
  577. }
  578. #endif
  579. static const struct dev_pm_ops davinci_mdio_pm_ops = {
  580. SET_RUNTIME_PM_OPS(davinci_mdio_runtime_suspend,
  581. davinci_mdio_runtime_resume, NULL)
  582. SET_LATE_SYSTEM_SLEEP_PM_OPS(davinci_mdio_suspend, davinci_mdio_resume)
  583. };
  584. static struct platform_driver davinci_mdio_driver = {
  585. .driver = {
  586. .name = "davinci_mdio",
  587. .pm = &davinci_mdio_pm_ops,
  588. .of_match_table = of_match_ptr(davinci_mdio_of_mtable),
  589. },
  590. .probe = davinci_mdio_probe,
  591. .remove = davinci_mdio_remove,
  592. };
  593. static int __init davinci_mdio_init(void)
  594. {
  595. return platform_driver_register(&davinci_mdio_driver);
  596. }
  597. device_initcall(davinci_mdio_init);
  598. static void __exit davinci_mdio_exit(void)
  599. {
  600. platform_driver_unregister(&davinci_mdio_driver);
  601. }
  602. module_exit(davinci_mdio_exit);
  603. MODULE_LICENSE("GPL");
  604. MODULE_DESCRIPTION("DaVinci MDIO driver");