spi-orion.c 13 KB

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