spi-orion.c 13 KB

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