spi-octeon.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011, 2012 Cavium, Inc.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <asm/octeon/octeon.h>
  17. #include <asm/octeon/cvmx-mpi-defs.h>
  18. #define OCTEON_SPI_CFG 0
  19. #define OCTEON_SPI_STS 0x08
  20. #define OCTEON_SPI_TX 0x10
  21. #define OCTEON_SPI_DAT0 0x80
  22. #define OCTEON_SPI_MAX_BYTES 9
  23. #define OCTEON_SPI_MAX_CLOCK_HZ 16000000
  24. struct octeon_spi {
  25. struct spi_master *my_master;
  26. u64 register_base;
  27. u64 last_cfg;
  28. u64 cs_enax;
  29. };
  30. struct octeon_spi_setup {
  31. u32 max_speed_hz;
  32. u8 chip_select;
  33. u8 mode;
  34. u8 bits_per_word;
  35. };
  36. static void octeon_spi_wait_ready(struct octeon_spi *p)
  37. {
  38. union cvmx_mpi_sts mpi_sts;
  39. unsigned int loops = 0;
  40. do {
  41. if (loops++)
  42. __delay(500);
  43. mpi_sts.u64 = cvmx_read_csr(p->register_base + OCTEON_SPI_STS);
  44. } while (mpi_sts.s.busy);
  45. }
  46. static int octeon_spi_do_transfer(struct octeon_spi *p,
  47. struct spi_message *msg,
  48. struct spi_transfer *xfer,
  49. bool last_xfer)
  50. {
  51. union cvmx_mpi_cfg mpi_cfg;
  52. union cvmx_mpi_tx mpi_tx;
  53. unsigned int clkdiv;
  54. unsigned int speed_hz;
  55. int mode;
  56. bool cpha, cpol;
  57. int bits_per_word;
  58. const u8 *tx_buf;
  59. u8 *rx_buf;
  60. int len;
  61. int i;
  62. struct octeon_spi_setup *msg_setup = spi_get_ctldata(msg->spi);
  63. speed_hz = msg_setup->max_speed_hz;
  64. mode = msg_setup->mode;
  65. cpha = mode & SPI_CPHA;
  66. cpol = mode & SPI_CPOL;
  67. bits_per_word = msg_setup->bits_per_word;
  68. if (xfer->speed_hz)
  69. speed_hz = xfer->speed_hz;
  70. if (xfer->bits_per_word)
  71. bits_per_word = xfer->bits_per_word;
  72. if (speed_hz > OCTEON_SPI_MAX_CLOCK_HZ)
  73. speed_hz = OCTEON_SPI_MAX_CLOCK_HZ;
  74. clkdiv = octeon_get_io_clock_rate() / (2 * speed_hz);
  75. mpi_cfg.u64 = 0;
  76. mpi_cfg.s.clkdiv = clkdiv;
  77. mpi_cfg.s.cshi = (mode & SPI_CS_HIGH) ? 1 : 0;
  78. mpi_cfg.s.lsbfirst = (mode & SPI_LSB_FIRST) ? 1 : 0;
  79. mpi_cfg.s.wireor = (mode & SPI_3WIRE) ? 1 : 0;
  80. mpi_cfg.s.idlelo = cpha != cpol;
  81. mpi_cfg.s.cslate = cpha ? 1 : 0;
  82. mpi_cfg.s.enable = 1;
  83. if (msg_setup->chip_select < 4)
  84. p->cs_enax |= 1ull << (12 + msg_setup->chip_select);
  85. mpi_cfg.u64 |= p->cs_enax;
  86. if (mpi_cfg.u64 != p->last_cfg) {
  87. p->last_cfg = mpi_cfg.u64;
  88. cvmx_write_csr(p->register_base + OCTEON_SPI_CFG, mpi_cfg.u64);
  89. }
  90. tx_buf = xfer->tx_buf;
  91. rx_buf = xfer->rx_buf;
  92. len = xfer->len;
  93. while (len > OCTEON_SPI_MAX_BYTES) {
  94. for (i = 0; i < OCTEON_SPI_MAX_BYTES; i++) {
  95. u8 d;
  96. if (tx_buf)
  97. d = *tx_buf++;
  98. else
  99. d = 0;
  100. cvmx_write_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i), d);
  101. }
  102. mpi_tx.u64 = 0;
  103. mpi_tx.s.csid = msg_setup->chip_select;
  104. mpi_tx.s.leavecs = 1;
  105. mpi_tx.s.txnum = tx_buf ? OCTEON_SPI_MAX_BYTES : 0;
  106. mpi_tx.s.totnum = OCTEON_SPI_MAX_BYTES;
  107. cvmx_write_csr(p->register_base + OCTEON_SPI_TX, mpi_tx.u64);
  108. octeon_spi_wait_ready(p);
  109. if (rx_buf)
  110. for (i = 0; i < OCTEON_SPI_MAX_BYTES; i++) {
  111. u64 v = cvmx_read_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i));
  112. *rx_buf++ = (u8)v;
  113. }
  114. len -= OCTEON_SPI_MAX_BYTES;
  115. }
  116. for (i = 0; i < len; i++) {
  117. u8 d;
  118. if (tx_buf)
  119. d = *tx_buf++;
  120. else
  121. d = 0;
  122. cvmx_write_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i), d);
  123. }
  124. mpi_tx.u64 = 0;
  125. mpi_tx.s.csid = msg_setup->chip_select;
  126. if (last_xfer)
  127. mpi_tx.s.leavecs = xfer->cs_change;
  128. else
  129. mpi_tx.s.leavecs = !xfer->cs_change;
  130. mpi_tx.s.txnum = tx_buf ? len : 0;
  131. mpi_tx.s.totnum = len;
  132. cvmx_write_csr(p->register_base + OCTEON_SPI_TX, mpi_tx.u64);
  133. octeon_spi_wait_ready(p);
  134. if (rx_buf)
  135. for (i = 0; i < len; i++) {
  136. u64 v = cvmx_read_csr(p->register_base + OCTEON_SPI_DAT0 + (8 * i));
  137. *rx_buf++ = (u8)v;
  138. }
  139. if (xfer->delay_usecs)
  140. udelay(xfer->delay_usecs);
  141. return xfer->len;
  142. }
  143. static int octeon_spi_validate_bpw(struct spi_device *spi, u32 speed)
  144. {
  145. switch (speed) {
  146. case 8:
  147. break;
  148. default:
  149. dev_err(&spi->dev, "Error: %d bits per word not supported\n",
  150. speed);
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. static int octeon_spi_transfer_one_message(struct spi_master *master,
  156. struct spi_message *msg)
  157. {
  158. struct octeon_spi *p = spi_master_get_devdata(master);
  159. unsigned int total_len = 0;
  160. int status = 0;
  161. struct spi_transfer *xfer;
  162. /*
  163. * We better have set the configuration via a call to .setup
  164. * before we get here.
  165. */
  166. if (spi_get_ctldata(msg->spi) == NULL) {
  167. status = -EINVAL;
  168. goto err;
  169. }
  170. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  171. if (xfer->bits_per_word) {
  172. status = octeon_spi_validate_bpw(msg->spi,
  173. xfer->bits_per_word);
  174. if (status)
  175. goto err;
  176. }
  177. }
  178. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  179. bool last_xfer = &xfer->transfer_list == msg->transfers.prev;
  180. int r = octeon_spi_do_transfer(p, msg, xfer, last_xfer);
  181. if (r < 0) {
  182. status = r;
  183. goto err;
  184. }
  185. total_len += r;
  186. }
  187. err:
  188. msg->status = status;
  189. msg->actual_length = total_len;
  190. spi_finalize_current_message(master);
  191. return status;
  192. }
  193. static struct octeon_spi_setup *octeon_spi_new_setup(struct spi_device *spi)
  194. {
  195. struct octeon_spi_setup *setup = kzalloc(sizeof(*setup), GFP_KERNEL);
  196. if (!setup)
  197. return NULL;
  198. setup->max_speed_hz = spi->max_speed_hz;
  199. setup->chip_select = spi->chip_select;
  200. setup->mode = spi->mode;
  201. setup->bits_per_word = spi->bits_per_word;
  202. return setup;
  203. }
  204. static int octeon_spi_setup(struct spi_device *spi)
  205. {
  206. int r;
  207. struct octeon_spi_setup *new_setup;
  208. struct octeon_spi_setup *old_setup = spi_get_ctldata(spi);
  209. r = octeon_spi_validate_bpw(spi, spi->bits_per_word);
  210. if (r)
  211. return r;
  212. new_setup = octeon_spi_new_setup(spi);
  213. if (!new_setup)
  214. return -ENOMEM;
  215. spi_set_ctldata(spi, new_setup);
  216. kfree(old_setup);
  217. return 0;
  218. }
  219. static void octeon_spi_cleanup(struct spi_device *spi)
  220. {
  221. struct octeon_spi_setup *old_setup = spi_get_ctldata(spi);
  222. spi_set_ctldata(spi, NULL);
  223. kfree(old_setup);
  224. }
  225. static int octeon_spi_nop_transfer_hardware(struct spi_master *master)
  226. {
  227. return 0;
  228. }
  229. static int octeon_spi_probe(struct platform_device *pdev)
  230. {
  231. struct resource *res_mem;
  232. struct spi_master *master;
  233. struct octeon_spi *p;
  234. int err = -ENOENT;
  235. master = spi_alloc_master(&pdev->dev, sizeof(struct octeon_spi));
  236. if (!master)
  237. return -ENOMEM;
  238. p = spi_master_get_devdata(master);
  239. platform_set_drvdata(pdev, p);
  240. p->my_master = master;
  241. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. if (res_mem == NULL) {
  243. dev_err(&pdev->dev, "found no memory resource\n");
  244. err = -ENXIO;
  245. goto fail;
  246. }
  247. if (!devm_request_mem_region(&pdev->dev, res_mem->start,
  248. resource_size(res_mem), res_mem->name)) {
  249. dev_err(&pdev->dev, "request_mem_region failed\n");
  250. goto fail;
  251. }
  252. p->register_base = (u64)devm_ioremap(&pdev->dev, res_mem->start,
  253. resource_size(res_mem));
  254. /* Dynamic bus numbering */
  255. master->bus_num = -1;
  256. master->num_chipselect = 4;
  257. master->mode_bits = SPI_CPHA |
  258. SPI_CPOL |
  259. SPI_CS_HIGH |
  260. SPI_LSB_FIRST |
  261. SPI_3WIRE;
  262. master->setup = octeon_spi_setup;
  263. master->cleanup = octeon_spi_cleanup;
  264. master->prepare_transfer_hardware = octeon_spi_nop_transfer_hardware;
  265. master->transfer_one_message = octeon_spi_transfer_one_message;
  266. master->unprepare_transfer_hardware = octeon_spi_nop_transfer_hardware;
  267. master->dev.of_node = pdev->dev.of_node;
  268. err = spi_register_master(master);
  269. if (err) {
  270. dev_err(&pdev->dev, "register master failed: %d\n", err);
  271. goto fail;
  272. }
  273. dev_info(&pdev->dev, "OCTEON SPI bus driver\n");
  274. return 0;
  275. fail:
  276. spi_master_put(master);
  277. return err;
  278. }
  279. static int octeon_spi_remove(struct platform_device *pdev)
  280. {
  281. struct octeon_spi *p = platform_get_drvdata(pdev);
  282. u64 register_base = p->register_base;
  283. spi_unregister_master(p->my_master);
  284. /* Clear the CSENA* and put everything in a known state. */
  285. cvmx_write_csr(register_base + OCTEON_SPI_CFG, 0);
  286. return 0;
  287. }
  288. static struct of_device_id octeon_spi_match[] = {
  289. { .compatible = "cavium,octeon-3010-spi", },
  290. {},
  291. };
  292. MODULE_DEVICE_TABLE(of, octeon_spi_match);
  293. static struct platform_driver octeon_spi_driver = {
  294. .driver = {
  295. .name = "spi-octeon",
  296. .owner = THIS_MODULE,
  297. .of_match_table = octeon_spi_match,
  298. },
  299. .probe = octeon_spi_probe,
  300. .remove = octeon_spi_remove,
  301. };
  302. module_platform_driver(octeon_spi_driver);
  303. MODULE_DESCRIPTION("Cavium, Inc. OCTEON SPI bus driver");
  304. MODULE_AUTHOR("David Daney");
  305. MODULE_LICENSE("GPL");