spi-orion.c 12 KB

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