spi-orion.c 14 KB

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