spi-orion.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Marvell Orion SPI controller driver
  3. *
  4. * Author: Shadi Ammouri <shadi@marvell.com>
  5. * Copyright (C) 2007-2008 Marvell Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/delay.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/module.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/of.h>
  20. #include <linux/clk.h>
  21. #include <linux/sizes.h>
  22. #include <asm/unaligned.h>
  23. #define DRIVER_NAME "orion_spi"
  24. /* Runtime PM autosuspend timeout: PM is fairly light on this driver */
  25. #define SPI_AUTOSUSPEND_TIMEOUT 200
  26. #define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
  27. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  28. #define ORION_SPI_IF_CTRL_REG 0x00
  29. #define ORION_SPI_IF_CONFIG_REG 0x04
  30. #define ORION_SPI_DATA_OUT_REG 0x08
  31. #define ORION_SPI_DATA_IN_REG 0x0c
  32. #define ORION_SPI_INT_CAUSE_REG 0x10
  33. #define ORION_SPI_MODE_CPOL (1 << 11)
  34. #define ORION_SPI_MODE_CPHA (1 << 12)
  35. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  36. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  37. #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
  38. ORION_SPI_MODE_CPHA)
  39. struct orion_spi {
  40. struct spi_master *master;
  41. void __iomem *base;
  42. struct clk *clk;
  43. };
  44. static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
  45. {
  46. return orion_spi->base + reg;
  47. }
  48. static inline void
  49. orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  50. {
  51. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  52. u32 val;
  53. val = readl(reg_addr);
  54. val |= mask;
  55. writel(val, reg_addr);
  56. }
  57. static inline void
  58. orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  59. {
  60. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  61. u32 val;
  62. val = readl(reg_addr);
  63. val &= ~mask;
  64. writel(val, reg_addr);
  65. }
  66. static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  67. {
  68. u32 tclk_hz;
  69. u32 rate;
  70. u32 prescale;
  71. u32 reg;
  72. struct orion_spi *orion_spi;
  73. orion_spi = spi_master_get_devdata(spi->master);
  74. tclk_hz = clk_get_rate(orion_spi->clk);
  75. /*
  76. * the supported rates are: 4,6,8...30
  77. * round up as we look for equal or less speed
  78. */
  79. rate = DIV_ROUND_UP(tclk_hz, speed);
  80. rate = roundup(rate, 2);
  81. /* check if requested speed is too small */
  82. if (rate > 30)
  83. return -EINVAL;
  84. if (rate < 4)
  85. rate = 4;
  86. /* Convert the rate to SPI clock divisor value. */
  87. prescale = 0x10 + rate/2;
  88. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  89. reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
  90. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  91. return 0;
  92. }
  93. static void
  94. orion_spi_mode_set(struct spi_device *spi)
  95. {
  96. u32 reg;
  97. struct orion_spi *orion_spi;
  98. orion_spi = spi_master_get_devdata(spi->master);
  99. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  100. reg &= ~ORION_SPI_MODE_MASK;
  101. if (spi->mode & SPI_CPOL)
  102. reg |= ORION_SPI_MODE_CPOL;
  103. if (spi->mode & SPI_CPHA)
  104. reg |= ORION_SPI_MODE_CPHA;
  105. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  106. }
  107. /*
  108. * called only when no transfer is active on the bus
  109. */
  110. static int
  111. orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  112. {
  113. struct orion_spi *orion_spi;
  114. unsigned int speed = spi->max_speed_hz;
  115. unsigned int bits_per_word = spi->bits_per_word;
  116. int rc;
  117. orion_spi = spi_master_get_devdata(spi->master);
  118. if ((t != NULL) && t->speed_hz)
  119. speed = t->speed_hz;
  120. if ((t != NULL) && t->bits_per_word)
  121. bits_per_word = t->bits_per_word;
  122. orion_spi_mode_set(spi);
  123. rc = orion_spi_baudrate_set(spi, speed);
  124. if (rc)
  125. return rc;
  126. if (bits_per_word == 16)
  127. orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  128. ORION_SPI_IF_8_16_BIT_MODE);
  129. else
  130. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  131. ORION_SPI_IF_8_16_BIT_MODE);
  132. return 0;
  133. }
  134. static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
  135. {
  136. if (enable)
  137. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  138. else
  139. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  140. }
  141. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  142. {
  143. int i;
  144. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  145. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  146. return 1;
  147. else
  148. udelay(1);
  149. }
  150. return -1;
  151. }
  152. static inline int
  153. orion_spi_write_read_8bit(struct spi_device *spi,
  154. const u8 **tx_buf, u8 **rx_buf)
  155. {
  156. void __iomem *tx_reg, *rx_reg, *int_reg;
  157. struct orion_spi *orion_spi;
  158. orion_spi = spi_master_get_devdata(spi->master);
  159. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  160. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  161. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  162. /* clear the interrupt cause register */
  163. writel(0x0, int_reg);
  164. if (tx_buf && *tx_buf)
  165. writel(*(*tx_buf)++, tx_reg);
  166. else
  167. writel(0, tx_reg);
  168. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  169. dev_err(&spi->dev, "TXS timed out\n");
  170. return -1;
  171. }
  172. if (rx_buf && *rx_buf)
  173. *(*rx_buf)++ = readl(rx_reg);
  174. return 1;
  175. }
  176. static inline int
  177. orion_spi_write_read_16bit(struct spi_device *spi,
  178. const u16 **tx_buf, u16 **rx_buf)
  179. {
  180. void __iomem *tx_reg, *rx_reg, *int_reg;
  181. struct orion_spi *orion_spi;
  182. orion_spi = spi_master_get_devdata(spi->master);
  183. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  184. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  185. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  186. /* clear the interrupt cause register */
  187. writel(0x0, int_reg);
  188. if (tx_buf && *tx_buf)
  189. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  190. else
  191. writel(0, tx_reg);
  192. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  193. dev_err(&spi->dev, "TXS timed out\n");
  194. return -1;
  195. }
  196. if (rx_buf && *rx_buf)
  197. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  198. return 1;
  199. }
  200. static unsigned int
  201. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  202. {
  203. unsigned int count;
  204. int word_len;
  205. word_len = spi->bits_per_word;
  206. count = xfer->len;
  207. if (word_len == 8) {
  208. const u8 *tx = xfer->tx_buf;
  209. u8 *rx = xfer->rx_buf;
  210. do {
  211. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  212. goto out;
  213. count--;
  214. } while (count);
  215. } else if (word_len == 16) {
  216. const u16 *tx = xfer->tx_buf;
  217. u16 *rx = xfer->rx_buf;
  218. do {
  219. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  220. goto out;
  221. count -= 2;
  222. } while (count);
  223. }
  224. out:
  225. return xfer->len - count;
  226. }
  227. static int orion_spi_transfer_one_message(struct spi_master *master,
  228. struct spi_message *m)
  229. {
  230. struct orion_spi *orion_spi = spi_master_get_devdata(master);
  231. struct spi_device *spi = m->spi;
  232. struct spi_transfer *t = NULL;
  233. int par_override = 0;
  234. int status = 0;
  235. int cs_active = 0;
  236. /* Load defaults */
  237. status = orion_spi_setup_transfer(spi, NULL);
  238. if (status < 0)
  239. goto msg_done;
  240. list_for_each_entry(t, &m->transfers, transfer_list) {
  241. if (par_override || t->speed_hz || t->bits_per_word) {
  242. par_override = 1;
  243. status = orion_spi_setup_transfer(spi, t);
  244. if (status < 0)
  245. break;
  246. if (!t->speed_hz && !t->bits_per_word)
  247. par_override = 0;
  248. }
  249. if (!cs_active) {
  250. orion_spi_set_cs(orion_spi, 1);
  251. cs_active = 1;
  252. }
  253. if (t->len)
  254. m->actual_length += orion_spi_write_read(spi, t);
  255. if (t->delay_usecs)
  256. udelay(t->delay_usecs);
  257. if (t->cs_change) {
  258. orion_spi_set_cs(orion_spi, 0);
  259. cs_active = 0;
  260. }
  261. }
  262. msg_done:
  263. if (cs_active)
  264. orion_spi_set_cs(orion_spi, 0);
  265. m->status = status;
  266. spi_finalize_current_message(master);
  267. return 0;
  268. }
  269. static int orion_spi_reset(struct orion_spi *orion_spi)
  270. {
  271. /* Verify that the CS is deasserted */
  272. orion_spi_set_cs(orion_spi, 0);
  273. return 0;
  274. }
  275. static int orion_spi_probe(struct platform_device *pdev)
  276. {
  277. struct spi_master *master;
  278. struct orion_spi *spi;
  279. struct resource *r;
  280. unsigned long tclk_hz;
  281. int status = 0;
  282. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  283. if (master == NULL) {
  284. dev_dbg(&pdev->dev, "master allocation failed\n");
  285. return -ENOMEM;
  286. }
  287. if (pdev->id != -1)
  288. master->bus_num = pdev->id;
  289. if (pdev->dev.of_node) {
  290. u32 cell_index;
  291. if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
  292. &cell_index))
  293. master->bus_num = cell_index;
  294. }
  295. /* we support only mode 0, and no options */
  296. master->mode_bits = SPI_CPHA | SPI_CPOL;
  297. master->transfer_one_message = orion_spi_transfer_one_message;
  298. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  299. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  300. master->auto_runtime_pm = true;
  301. platform_set_drvdata(pdev, master);
  302. spi = spi_master_get_devdata(master);
  303. spi->master = master;
  304. spi->clk = devm_clk_get(&pdev->dev, NULL);
  305. if (IS_ERR(spi->clk)) {
  306. status = PTR_ERR(spi->clk);
  307. goto out;
  308. }
  309. status = clk_prepare_enable(spi->clk);
  310. if (status)
  311. goto out;
  312. tclk_hz = clk_get_rate(spi->clk);
  313. master->max_speed_hz = DIV_ROUND_UP(tclk_hz, 4);
  314. master->min_speed_hz = DIV_ROUND_UP(tclk_hz, 30);
  315. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  316. spi->base = devm_ioremap_resource(&pdev->dev, r);
  317. if (IS_ERR(spi->base)) {
  318. status = PTR_ERR(spi->base);
  319. goto out_rel_clk;
  320. }
  321. pm_runtime_set_active(&pdev->dev);
  322. pm_runtime_use_autosuspend(&pdev->dev);
  323. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  324. pm_runtime_enable(&pdev->dev);
  325. status = orion_spi_reset(spi);
  326. if (status < 0)
  327. goto out_rel_pm;
  328. pm_runtime_mark_last_busy(&pdev->dev);
  329. pm_runtime_put_autosuspend(&pdev->dev);
  330. master->dev.of_node = pdev->dev.of_node;
  331. status = spi_register_master(master);
  332. if (status < 0)
  333. goto out_rel_pm;
  334. return status;
  335. out_rel_pm:
  336. pm_runtime_disable(&pdev->dev);
  337. out_rel_clk:
  338. clk_disable_unprepare(spi->clk);
  339. out:
  340. spi_master_put(master);
  341. return status;
  342. }
  343. static int orion_spi_remove(struct platform_device *pdev)
  344. {
  345. struct spi_master *master = platform_get_drvdata(pdev);
  346. struct orion_spi *spi = spi_master_get_devdata(master);
  347. pm_runtime_get_sync(&pdev->dev);
  348. clk_disable_unprepare(spi->clk);
  349. spi_unregister_master(master);
  350. pm_runtime_disable(&pdev->dev);
  351. return 0;
  352. }
  353. MODULE_ALIAS("platform:" DRIVER_NAME);
  354. #ifdef CONFIG_PM_RUNTIME
  355. static int orion_spi_runtime_suspend(struct device *dev)
  356. {
  357. struct spi_master *master = dev_get_drvdata(dev);
  358. struct orion_spi *spi = spi_master_get_devdata(master);
  359. clk_disable_unprepare(spi->clk);
  360. return 0;
  361. }
  362. static int orion_spi_runtime_resume(struct device *dev)
  363. {
  364. struct spi_master *master = dev_get_drvdata(dev);
  365. struct orion_spi *spi = spi_master_get_devdata(master);
  366. return clk_prepare_enable(spi->clk);
  367. }
  368. #endif
  369. static const struct dev_pm_ops orion_spi_pm_ops = {
  370. SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
  371. orion_spi_runtime_resume,
  372. NULL)
  373. };
  374. static const struct of_device_id orion_spi_of_match_table[] = {
  375. { .compatible = "marvell,orion-spi", },
  376. {}
  377. };
  378. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  379. static struct platform_driver orion_spi_driver = {
  380. .driver = {
  381. .name = DRIVER_NAME,
  382. .owner = THIS_MODULE,
  383. .pm = &orion_spi_pm_ops,
  384. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  385. },
  386. .probe = orion_spi_probe,
  387. .remove = orion_spi_remove,
  388. };
  389. module_platform_driver(orion_spi_driver);
  390. MODULE_DESCRIPTION("Orion SPI driver");
  391. MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
  392. MODULE_LICENSE("GPL");