spi-orion.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/clk.h>
  21. #include <asm/unaligned.h>
  22. #define DRIVER_NAME "orion_spi"
  23. #define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
  24. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  25. #define ORION_SPI_IF_CTRL_REG 0x00
  26. #define ORION_SPI_IF_CONFIG_REG 0x04
  27. #define ORION_SPI_DATA_OUT_REG 0x08
  28. #define ORION_SPI_DATA_IN_REG 0x0c
  29. #define ORION_SPI_INT_CAUSE_REG 0x10
  30. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  31. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  32. struct orion_spi {
  33. struct spi_master *master;
  34. void __iomem *base;
  35. unsigned int max_speed;
  36. unsigned int min_speed;
  37. struct clk *clk;
  38. };
  39. static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
  40. {
  41. return orion_spi->base + reg;
  42. }
  43. static inline void
  44. orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  45. {
  46. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  47. u32 val;
  48. val = readl(reg_addr);
  49. val |= mask;
  50. writel(val, reg_addr);
  51. }
  52. static inline void
  53. orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  54. {
  55. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  56. u32 val;
  57. val = readl(reg_addr);
  58. val &= ~mask;
  59. writel(val, reg_addr);
  60. }
  61. static int orion_spi_set_transfer_size(struct orion_spi *orion_spi, int size)
  62. {
  63. if (size == 16) {
  64. orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  65. ORION_SPI_IF_8_16_BIT_MODE);
  66. } else if (size == 8) {
  67. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  68. ORION_SPI_IF_8_16_BIT_MODE);
  69. } else {
  70. pr_debug("Bad bits per word value %d (only 8 or 16 are "
  71. "allowed).\n", size);
  72. return -EINVAL;
  73. }
  74. return 0;
  75. }
  76. static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  77. {
  78. u32 tclk_hz;
  79. u32 rate;
  80. u32 prescale;
  81. u32 reg;
  82. struct orion_spi *orion_spi;
  83. orion_spi = spi_master_get_devdata(spi->master);
  84. tclk_hz = clk_get_rate(orion_spi->clk);
  85. /*
  86. * the supported rates are: 4,6,8...30
  87. * round up as we look for equal or less speed
  88. */
  89. rate = DIV_ROUND_UP(tclk_hz, speed);
  90. rate = roundup(rate, 2);
  91. /* check if requested speed is too small */
  92. if (rate > 30)
  93. return -EINVAL;
  94. if (rate < 4)
  95. rate = 4;
  96. /* Convert the rate to SPI clock divisor value. */
  97. prescale = 0x10 + rate/2;
  98. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  99. reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale);
  100. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  101. return 0;
  102. }
  103. /*
  104. * called only when no transfer is active on the bus
  105. */
  106. static int
  107. orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  108. {
  109. struct orion_spi *orion_spi;
  110. unsigned int speed = spi->max_speed_hz;
  111. unsigned int bits_per_word = spi->bits_per_word;
  112. int rc;
  113. orion_spi = spi_master_get_devdata(spi->master);
  114. if ((t != NULL) && t->speed_hz)
  115. speed = t->speed_hz;
  116. if ((t != NULL) && t->bits_per_word)
  117. bits_per_word = t->bits_per_word;
  118. rc = orion_spi_baudrate_set(spi, speed);
  119. if (rc)
  120. return rc;
  121. return orion_spi_set_transfer_size(orion_spi, bits_per_word);
  122. }
  123. static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
  124. {
  125. if (enable)
  126. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  127. else
  128. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  129. }
  130. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  131. {
  132. int i;
  133. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  134. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  135. return 1;
  136. else
  137. udelay(1);
  138. }
  139. return -1;
  140. }
  141. static inline int
  142. orion_spi_write_read_8bit(struct spi_device *spi,
  143. const u8 **tx_buf, u8 **rx_buf)
  144. {
  145. void __iomem *tx_reg, *rx_reg, *int_reg;
  146. struct orion_spi *orion_spi;
  147. orion_spi = spi_master_get_devdata(spi->master);
  148. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  149. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  150. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  151. /* clear the interrupt cause register */
  152. writel(0x0, int_reg);
  153. if (tx_buf && *tx_buf)
  154. writel(*(*tx_buf)++, tx_reg);
  155. else
  156. writel(0, tx_reg);
  157. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  158. dev_err(&spi->dev, "TXS timed out\n");
  159. return -1;
  160. }
  161. if (rx_buf && *rx_buf)
  162. *(*rx_buf)++ = readl(rx_reg);
  163. return 1;
  164. }
  165. static inline int
  166. orion_spi_write_read_16bit(struct spi_device *spi,
  167. const u16 **tx_buf, u16 **rx_buf)
  168. {
  169. void __iomem *tx_reg, *rx_reg, *int_reg;
  170. struct orion_spi *orion_spi;
  171. orion_spi = spi_master_get_devdata(spi->master);
  172. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  173. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  174. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  175. /* clear the interrupt cause register */
  176. writel(0x0, int_reg);
  177. if (tx_buf && *tx_buf)
  178. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  179. else
  180. writel(0, tx_reg);
  181. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  182. dev_err(&spi->dev, "TXS timed out\n");
  183. return -1;
  184. }
  185. if (rx_buf && *rx_buf)
  186. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  187. return 1;
  188. }
  189. static unsigned int
  190. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  191. {
  192. struct orion_spi *orion_spi;
  193. unsigned int count;
  194. int word_len;
  195. orion_spi = spi_master_get_devdata(spi->master);
  196. word_len = spi->bits_per_word;
  197. count = xfer->len;
  198. if (word_len == 8) {
  199. const u8 *tx = xfer->tx_buf;
  200. u8 *rx = xfer->rx_buf;
  201. do {
  202. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  203. goto out;
  204. count--;
  205. } while (count);
  206. } else if (word_len == 16) {
  207. const u16 *tx = xfer->tx_buf;
  208. u16 *rx = xfer->rx_buf;
  209. do {
  210. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  211. goto out;
  212. count -= 2;
  213. } while (count);
  214. }
  215. out:
  216. return xfer->len - count;
  217. }
  218. static int orion_spi_transfer_one_message(struct spi_master *master,
  219. struct spi_message *m)
  220. {
  221. struct orion_spi *orion_spi = spi_master_get_devdata(master);
  222. struct spi_device *spi = m->spi;
  223. struct spi_transfer *t = NULL;
  224. int par_override = 0;
  225. int status = 0;
  226. int cs_active = 0;
  227. /* Load defaults */
  228. status = orion_spi_setup_transfer(spi, NULL);
  229. if (status < 0)
  230. goto msg_done;
  231. list_for_each_entry(t, &m->transfers, transfer_list) {
  232. /* make sure buffer length is even when working in 16
  233. * bit mode*/
  234. if ((t->bits_per_word == 16) && (t->len & 1)) {
  235. dev_err(&spi->dev,
  236. "message rejected : "
  237. "odd data length %d while in 16 bit mode\n",
  238. t->len);
  239. status = -EIO;
  240. goto msg_done;
  241. }
  242. if (t->speed_hz && t->speed_hz < orion_spi->min_speed) {
  243. dev_err(&spi->dev,
  244. "message rejected : "
  245. "device min speed (%d Hz) exceeds "
  246. "required transfer speed (%d Hz)\n",
  247. orion_spi->min_speed, t->speed_hz);
  248. status = -EIO;
  249. goto msg_done;
  250. }
  251. if (par_override || t->speed_hz || t->bits_per_word) {
  252. par_override = 1;
  253. status = orion_spi_setup_transfer(spi, t);
  254. if (status < 0)
  255. break;
  256. if (!t->speed_hz && !t->bits_per_word)
  257. par_override = 0;
  258. }
  259. if (!cs_active) {
  260. orion_spi_set_cs(orion_spi, 1);
  261. cs_active = 1;
  262. }
  263. if (t->len)
  264. m->actual_length += orion_spi_write_read(spi, t);
  265. if (t->delay_usecs)
  266. udelay(t->delay_usecs);
  267. if (t->cs_change) {
  268. orion_spi_set_cs(orion_spi, 0);
  269. cs_active = 0;
  270. }
  271. }
  272. msg_done:
  273. if (cs_active)
  274. orion_spi_set_cs(orion_spi, 0);
  275. m->status = status;
  276. spi_finalize_current_message(master);
  277. return 0;
  278. }
  279. static int __init orion_spi_reset(struct orion_spi *orion_spi)
  280. {
  281. /* Verify that the CS is deasserted */
  282. orion_spi_set_cs(orion_spi, 0);
  283. return 0;
  284. }
  285. static int orion_spi_setup(struct spi_device *spi)
  286. {
  287. struct orion_spi *orion_spi;
  288. orion_spi = spi_master_get_devdata(spi->master);
  289. if ((spi->max_speed_hz == 0)
  290. || (spi->max_speed_hz > orion_spi->max_speed))
  291. spi->max_speed_hz = orion_spi->max_speed;
  292. if (spi->max_speed_hz < orion_spi->min_speed) {
  293. dev_err(&spi->dev, "setup: requested speed too low %d Hz\n",
  294. spi->max_speed_hz);
  295. return -EINVAL;
  296. }
  297. /*
  298. * baudrate & width will be set orion_spi_setup_transfer
  299. */
  300. return 0;
  301. }
  302. static int __init orion_spi_probe(struct platform_device *pdev)
  303. {
  304. struct spi_master *master;
  305. struct orion_spi *spi;
  306. struct resource *r;
  307. unsigned long tclk_hz;
  308. int status = 0;
  309. const u32 *iprop;
  310. int size;
  311. master = spi_alloc_master(&pdev->dev, sizeof *spi);
  312. if (master == NULL) {
  313. dev_dbg(&pdev->dev, "master allocation failed\n");
  314. return -ENOMEM;
  315. }
  316. if (pdev->id != -1)
  317. master->bus_num = pdev->id;
  318. if (pdev->dev.of_node) {
  319. iprop = of_get_property(pdev->dev.of_node, "cell-index",
  320. &size);
  321. if (iprop && size == sizeof(*iprop))
  322. master->bus_num = *iprop;
  323. }
  324. /* we support only mode 0, and no options */
  325. master->mode_bits = 0;
  326. master->setup = orion_spi_setup;
  327. master->transfer_one_message = orion_spi_transfer_one_message;
  328. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  329. dev_set_drvdata(&pdev->dev, master);
  330. spi = spi_master_get_devdata(master);
  331. spi->master = master;
  332. spi->clk = clk_get(&pdev->dev, NULL);
  333. if (IS_ERR(spi->clk)) {
  334. status = PTR_ERR(spi->clk);
  335. goto out;
  336. }
  337. clk_prepare(spi->clk);
  338. clk_enable(spi->clk);
  339. tclk_hz = clk_get_rate(spi->clk);
  340. spi->max_speed = DIV_ROUND_UP(tclk_hz, 4);
  341. spi->min_speed = DIV_ROUND_UP(tclk_hz, 30);
  342. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  343. if (r == NULL) {
  344. status = -ENODEV;
  345. goto out_rel_clk;
  346. }
  347. if (!request_mem_region(r->start, resource_size(r),
  348. dev_name(&pdev->dev))) {
  349. status = -EBUSY;
  350. goto out_rel_clk;
  351. }
  352. spi->base = ioremap(r->start, SZ_1K);
  353. if (orion_spi_reset(spi) < 0)
  354. goto out_rel_mem;
  355. master->dev.of_node = pdev->dev.of_node;
  356. status = spi_register_master(master);
  357. if (status < 0)
  358. goto out_rel_mem;
  359. return status;
  360. out_rel_mem:
  361. release_mem_region(r->start, resource_size(r));
  362. out_rel_clk:
  363. clk_disable_unprepare(spi->clk);
  364. clk_put(spi->clk);
  365. out:
  366. spi_master_put(master);
  367. return status;
  368. }
  369. static int __exit orion_spi_remove(struct platform_device *pdev)
  370. {
  371. struct spi_master *master;
  372. struct resource *r;
  373. struct orion_spi *spi;
  374. master = dev_get_drvdata(&pdev->dev);
  375. spi = spi_master_get_devdata(master);
  376. clk_disable_unprepare(spi->clk);
  377. clk_put(spi->clk);
  378. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  379. release_mem_region(r->start, resource_size(r));
  380. spi_unregister_master(master);
  381. return 0;
  382. }
  383. MODULE_ALIAS("platform:" DRIVER_NAME);
  384. static const struct of_device_id orion_spi_of_match_table[] __devinitdata = {
  385. { .compatible = "marvell,orion-spi", },
  386. {}
  387. };
  388. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  389. static struct platform_driver orion_spi_driver = {
  390. .driver = {
  391. .name = DRIVER_NAME,
  392. .owner = THIS_MODULE,
  393. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  394. },
  395. .remove = __exit_p(orion_spi_remove),
  396. };
  397. static int __init orion_spi_init(void)
  398. {
  399. return platform_driver_probe(&orion_spi_driver, orion_spi_probe);
  400. }
  401. module_init(orion_spi_init);
  402. static void __exit orion_spi_exit(void)
  403. {
  404. platform_driver_unregister(&orion_spi_driver);
  405. }
  406. module_exit(orion_spi_exit);
  407. MODULE_DESCRIPTION("Orion SPI driver");
  408. MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
  409. MODULE_LICENSE("GPL");