spi-orion.c 14 KB

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