spi_sh_msiof.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * SuperH MSIOF SPI Master Interface
  3. *
  4. * Copyright (c) 2009 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/bitmap.h>
  12. #include <linux/clk.h>
  13. #include <linux/completion.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/spi/sh_msiof.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <asm/unaligned.h>
  27. struct sh_msiof_spi_priv {
  28. struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
  29. void __iomem *mapbase;
  30. struct clk *clk;
  31. struct platform_device *pdev;
  32. struct sh_msiof_spi_info *info;
  33. struct completion done;
  34. unsigned long flags;
  35. int tx_fifo_size;
  36. int rx_fifo_size;
  37. };
  38. #define TMDR1 0x00
  39. #define TMDR2 0x04
  40. #define TMDR3 0x08
  41. #define RMDR1 0x10
  42. #define RMDR2 0x14
  43. #define RMDR3 0x18
  44. #define TSCR 0x20
  45. #define RSCR 0x22
  46. #define CTR 0x28
  47. #define FCTR 0x30
  48. #define STR 0x40
  49. #define IER 0x44
  50. #define TDR1 0x48
  51. #define TDR2 0x4c
  52. #define TFDR 0x50
  53. #define RDR1 0x58
  54. #define RDR2 0x5c
  55. #define RFDR 0x60
  56. #define CTR_TSCKE (1 << 15)
  57. #define CTR_TFSE (1 << 14)
  58. #define CTR_TXE (1 << 9)
  59. #define CTR_RXE (1 << 8)
  60. #define STR_TEOF (1 << 23)
  61. #define STR_REOF (1 << 7)
  62. static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
  63. {
  64. switch (reg_offs) {
  65. case TSCR:
  66. case RSCR:
  67. return ioread16(p->mapbase + reg_offs);
  68. default:
  69. return ioread32(p->mapbase + reg_offs);
  70. }
  71. }
  72. static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
  73. u32 value)
  74. {
  75. switch (reg_offs) {
  76. case TSCR:
  77. case RSCR:
  78. iowrite16(value, p->mapbase + reg_offs);
  79. break;
  80. default:
  81. iowrite32(value, p->mapbase + reg_offs);
  82. break;
  83. }
  84. }
  85. static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
  86. u32 clr, u32 set)
  87. {
  88. u32 mask = clr | set;
  89. u32 data;
  90. int k;
  91. data = sh_msiof_read(p, CTR);
  92. data &= ~clr;
  93. data |= set;
  94. sh_msiof_write(p, CTR, data);
  95. for (k = 100; k > 0; k--) {
  96. if ((sh_msiof_read(p, CTR) & mask) == set)
  97. break;
  98. udelay(10);
  99. }
  100. return k > 0 ? 0 : -ETIMEDOUT;
  101. }
  102. static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
  103. {
  104. struct sh_msiof_spi_priv *p = data;
  105. /* just disable the interrupt and wake up */
  106. sh_msiof_write(p, IER, 0);
  107. complete(&p->done);
  108. return IRQ_HANDLED;
  109. }
  110. static struct {
  111. unsigned short div;
  112. unsigned short scr;
  113. } const sh_msiof_spi_clk_table[] = {
  114. { 1, 0x0007 },
  115. { 2, 0x0000 },
  116. { 4, 0x0001 },
  117. { 8, 0x0002 },
  118. { 16, 0x0003 },
  119. { 32, 0x0004 },
  120. { 64, 0x1f00 },
  121. { 128, 0x1f01 },
  122. { 256, 0x1f02 },
  123. { 512, 0x1f03 },
  124. { 1024, 0x1f04 },
  125. };
  126. static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
  127. unsigned long parent_rate,
  128. unsigned long spi_hz)
  129. {
  130. unsigned long div = 1024;
  131. size_t k;
  132. if (!WARN_ON(!spi_hz || !parent_rate))
  133. div = parent_rate / spi_hz;
  134. /* TODO: make more fine grained */
  135. for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
  136. if (sh_msiof_spi_clk_table[k].div >= div)
  137. break;
  138. }
  139. k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
  140. sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
  141. sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
  142. }
  143. static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
  144. u32 cpol, u32 cpha,
  145. u32 tx_hi_z, u32 lsb_first)
  146. {
  147. u32 tmp;
  148. int edge;
  149. /*
  150. * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
  151. * 0 0 10 10 1 1
  152. * 0 1 10 10 0 0
  153. * 1 0 11 11 0 0
  154. * 1 1 11 11 1 1
  155. */
  156. sh_msiof_write(p, FCTR, 0);
  157. sh_msiof_write(p, TMDR1, 0xe2000005 | (lsb_first << 24));
  158. sh_msiof_write(p, RMDR1, 0x22000005 | (lsb_first << 24));
  159. tmp = 0xa0000000;
  160. tmp |= cpol << 30; /* TSCKIZ */
  161. tmp |= cpol << 28; /* RSCKIZ */
  162. edge = cpol ^ !cpha;
  163. tmp |= edge << 27; /* TEDG */
  164. tmp |= edge << 26; /* REDG */
  165. tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */
  166. sh_msiof_write(p, CTR, tmp);
  167. }
  168. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  169. const void *tx_buf, void *rx_buf,
  170. u32 bits, u32 words)
  171. {
  172. u32 dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
  173. if (tx_buf)
  174. sh_msiof_write(p, TMDR2, dr2);
  175. else
  176. sh_msiof_write(p, TMDR2, dr2 | 1);
  177. if (rx_buf)
  178. sh_msiof_write(p, RMDR2, dr2);
  179. sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
  180. }
  181. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  182. {
  183. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  184. }
  185. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  186. const void *tx_buf, int words, int fs)
  187. {
  188. const u8 *buf_8 = tx_buf;
  189. int k;
  190. for (k = 0; k < words; k++)
  191. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  192. }
  193. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  194. const void *tx_buf, int words, int fs)
  195. {
  196. const u16 *buf_16 = tx_buf;
  197. int k;
  198. for (k = 0; k < words; k++)
  199. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  200. }
  201. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  202. const void *tx_buf, int words, int fs)
  203. {
  204. const u16 *buf_16 = tx_buf;
  205. int k;
  206. for (k = 0; k < words; k++)
  207. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  208. }
  209. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  210. const void *tx_buf, int words, int fs)
  211. {
  212. const u32 *buf_32 = tx_buf;
  213. int k;
  214. for (k = 0; k < words; k++)
  215. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  216. }
  217. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  218. const void *tx_buf, int words, int fs)
  219. {
  220. const u32 *buf_32 = tx_buf;
  221. int k;
  222. for (k = 0; k < words; k++)
  223. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  224. }
  225. static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
  226. const void *tx_buf, int words, int fs)
  227. {
  228. const u32 *buf_32 = tx_buf;
  229. int k;
  230. for (k = 0; k < words; k++)
  231. sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
  232. }
  233. static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
  234. const void *tx_buf, int words, int fs)
  235. {
  236. const u32 *buf_32 = tx_buf;
  237. int k;
  238. for (k = 0; k < words; k++)
  239. sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
  240. }
  241. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  242. void *rx_buf, int words, int fs)
  243. {
  244. u8 *buf_8 = rx_buf;
  245. int k;
  246. for (k = 0; k < words; k++)
  247. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  248. }
  249. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  250. void *rx_buf, int words, int fs)
  251. {
  252. u16 *buf_16 = rx_buf;
  253. int k;
  254. for (k = 0; k < words; k++)
  255. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  256. }
  257. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  258. void *rx_buf, int words, int fs)
  259. {
  260. u16 *buf_16 = rx_buf;
  261. int k;
  262. for (k = 0; k < words; k++)
  263. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  264. }
  265. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  266. void *rx_buf, int words, int fs)
  267. {
  268. u32 *buf_32 = rx_buf;
  269. int k;
  270. for (k = 0; k < words; k++)
  271. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  272. }
  273. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  274. void *rx_buf, int words, int fs)
  275. {
  276. u32 *buf_32 = rx_buf;
  277. int k;
  278. for (k = 0; k < words; k++)
  279. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  280. }
  281. static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
  282. void *rx_buf, int words, int fs)
  283. {
  284. u32 *buf_32 = rx_buf;
  285. int k;
  286. for (k = 0; k < words; k++)
  287. buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
  288. }
  289. static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
  290. void *rx_buf, int words, int fs)
  291. {
  292. u32 *buf_32 = rx_buf;
  293. int k;
  294. for (k = 0; k < words; k++)
  295. put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
  296. }
  297. static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
  298. {
  299. int bits;
  300. bits = t ? t->bits_per_word : 0;
  301. if (!bits)
  302. bits = spi->bits_per_word;
  303. return bits;
  304. }
  305. static unsigned long sh_msiof_spi_hz(struct spi_device *spi,
  306. struct spi_transfer *t)
  307. {
  308. unsigned long hz;
  309. hz = t ? t->speed_hz : 0;
  310. if (!hz)
  311. hz = spi->max_speed_hz;
  312. return hz;
  313. }
  314. static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
  315. struct spi_transfer *t)
  316. {
  317. int bits;
  318. /* noting to check hz values against since parent clock is disabled */
  319. bits = sh_msiof_spi_bits(spi, t);
  320. if (bits < 8)
  321. return -EINVAL;
  322. if (bits > 32)
  323. return -EINVAL;
  324. return spi_bitbang_setup_transfer(spi, t);
  325. }
  326. static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
  327. {
  328. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  329. int value;
  330. /* chip select is active low unless SPI_CS_HIGH is set */
  331. if (spi->mode & SPI_CS_HIGH)
  332. value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
  333. else
  334. value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
  335. if (is_on == BITBANG_CS_ACTIVE) {
  336. if (!test_and_set_bit(0, &p->flags)) {
  337. pm_runtime_get_sync(&p->pdev->dev);
  338. clk_enable(p->clk);
  339. }
  340. /* Configure pins before asserting CS */
  341. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  342. !!(spi->mode & SPI_CPHA),
  343. !!(spi->mode & SPI_3WIRE),
  344. !!(spi->mode & SPI_LSB_FIRST));
  345. }
  346. /* use spi->controller data for CS (same strategy as spi_gpio) */
  347. gpio_set_value((unsigned)spi->controller_data, value);
  348. if (is_on == BITBANG_CS_INACTIVE) {
  349. if (test_and_clear_bit(0, &p->flags)) {
  350. clk_disable(p->clk);
  351. pm_runtime_put(&p->pdev->dev);
  352. }
  353. }
  354. }
  355. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  356. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  357. const void *, int, int),
  358. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  359. void *, int, int),
  360. const void *tx_buf, void *rx_buf,
  361. int words, int bits)
  362. {
  363. int fifo_shift;
  364. int ret;
  365. /* limit maximum word transfer to rx/tx fifo size */
  366. if (tx_buf)
  367. words = min_t(int, words, p->tx_fifo_size);
  368. if (rx_buf)
  369. words = min_t(int, words, p->rx_fifo_size);
  370. /* the fifo contents need shifting */
  371. fifo_shift = 32 - bits;
  372. /* setup msiof transfer mode registers */
  373. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  374. /* write tx fifo */
  375. if (tx_buf)
  376. tx_fifo(p, tx_buf, words, fifo_shift);
  377. /* setup clock and rx/tx signals */
  378. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  379. if (rx_buf)
  380. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  381. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  382. /* start by setting frame bit */
  383. INIT_COMPLETION(p->done);
  384. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  385. if (ret) {
  386. dev_err(&p->pdev->dev, "failed to start hardware\n");
  387. goto err;
  388. }
  389. /* wait for tx fifo to be emptied / rx fifo to be filled */
  390. wait_for_completion(&p->done);
  391. /* read rx fifo */
  392. if (rx_buf)
  393. rx_fifo(p, rx_buf, words, fifo_shift);
  394. /* clear status bits */
  395. sh_msiof_reset_str(p);
  396. /* shut down frame, tx/tx and clock signals */
  397. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  398. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  399. if (rx_buf)
  400. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  401. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  402. if (ret) {
  403. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  404. goto err;
  405. }
  406. return words;
  407. err:
  408. sh_msiof_write(p, IER, 0);
  409. return ret;
  410. }
  411. static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  412. {
  413. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  414. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  415. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  416. int bits;
  417. int bytes_per_word;
  418. int bytes_done;
  419. int words;
  420. int n;
  421. bool swab;
  422. bits = sh_msiof_spi_bits(spi, t);
  423. if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
  424. bits = 32;
  425. swab = true;
  426. } else {
  427. swab = false;
  428. }
  429. /* setup bytes per word and fifo read/write functions */
  430. if (bits <= 8) {
  431. bytes_per_word = 1;
  432. tx_fifo = sh_msiof_spi_write_fifo_8;
  433. rx_fifo = sh_msiof_spi_read_fifo_8;
  434. } else if (bits <= 16) {
  435. bytes_per_word = 2;
  436. if ((unsigned long)t->tx_buf & 0x01)
  437. tx_fifo = sh_msiof_spi_write_fifo_16u;
  438. else
  439. tx_fifo = sh_msiof_spi_write_fifo_16;
  440. if ((unsigned long)t->rx_buf & 0x01)
  441. rx_fifo = sh_msiof_spi_read_fifo_16u;
  442. else
  443. rx_fifo = sh_msiof_spi_read_fifo_16;
  444. } else if (swab) {
  445. bytes_per_word = 4;
  446. if ((unsigned long)t->tx_buf & 0x03)
  447. tx_fifo = sh_msiof_spi_write_fifo_s32u;
  448. else
  449. tx_fifo = sh_msiof_spi_write_fifo_s32;
  450. if ((unsigned long)t->rx_buf & 0x03)
  451. rx_fifo = sh_msiof_spi_read_fifo_s32u;
  452. else
  453. rx_fifo = sh_msiof_spi_read_fifo_s32;
  454. } else {
  455. bytes_per_word = 4;
  456. if ((unsigned long)t->tx_buf & 0x03)
  457. tx_fifo = sh_msiof_spi_write_fifo_32u;
  458. else
  459. tx_fifo = sh_msiof_spi_write_fifo_32;
  460. if ((unsigned long)t->rx_buf & 0x03)
  461. rx_fifo = sh_msiof_spi_read_fifo_32u;
  462. else
  463. rx_fifo = sh_msiof_spi_read_fifo_32;
  464. }
  465. /* setup clocks (clock already enabled in chipselect()) */
  466. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
  467. sh_msiof_spi_hz(spi, t));
  468. /* transfer in fifo sized chunks */
  469. words = t->len / bytes_per_word;
  470. bytes_done = 0;
  471. while (bytes_done < t->len) {
  472. void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
  473. const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
  474. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
  475. tx_buf,
  476. rx_buf,
  477. words, bits);
  478. if (n < 0)
  479. break;
  480. bytes_done += n * bytes_per_word;
  481. words -= n;
  482. }
  483. return bytes_done;
  484. }
  485. static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
  486. u32 word, u8 bits)
  487. {
  488. BUG(); /* unused but needed by bitbang code */
  489. return 0;
  490. }
  491. static int sh_msiof_spi_probe(struct platform_device *pdev)
  492. {
  493. struct resource *r;
  494. struct spi_master *master;
  495. struct sh_msiof_spi_priv *p;
  496. char clk_name[16];
  497. int i;
  498. int ret;
  499. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  500. if (master == NULL) {
  501. dev_err(&pdev->dev, "failed to allocate spi master\n");
  502. ret = -ENOMEM;
  503. goto err0;
  504. }
  505. p = spi_master_get_devdata(master);
  506. platform_set_drvdata(pdev, p);
  507. p->info = pdev->dev.platform_data;
  508. init_completion(&p->done);
  509. snprintf(clk_name, sizeof(clk_name), "msiof%d", pdev->id);
  510. p->clk = clk_get(&pdev->dev, clk_name);
  511. if (IS_ERR(p->clk)) {
  512. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  513. ret = PTR_ERR(p->clk);
  514. goto err1;
  515. }
  516. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  517. i = platform_get_irq(pdev, 0);
  518. if (!r || i < 0) {
  519. dev_err(&pdev->dev, "cannot get platform resources\n");
  520. ret = -ENOENT;
  521. goto err2;
  522. }
  523. p->mapbase = ioremap_nocache(r->start, resource_size(r));
  524. if (!p->mapbase) {
  525. dev_err(&pdev->dev, "unable to ioremap\n");
  526. ret = -ENXIO;
  527. goto err2;
  528. }
  529. ret = request_irq(i, sh_msiof_spi_irq, IRQF_DISABLED,
  530. dev_name(&pdev->dev), p);
  531. if (ret) {
  532. dev_err(&pdev->dev, "unable to request irq\n");
  533. goto err3;
  534. }
  535. p->pdev = pdev;
  536. pm_runtime_enable(&pdev->dev);
  537. /* The standard version of MSIOF use 64 word FIFOs */
  538. p->tx_fifo_size = 64;
  539. p->rx_fifo_size = 64;
  540. /* Platform data may override FIFO sizes */
  541. if (p->info->tx_fifo_override)
  542. p->tx_fifo_size = p->info->tx_fifo_override;
  543. if (p->info->rx_fifo_override)
  544. p->rx_fifo_size = p->info->rx_fifo_override;
  545. /* init master and bitbang code */
  546. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  547. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  548. master->flags = 0;
  549. master->bus_num = pdev->id;
  550. master->num_chipselect = p->info->num_chipselect;
  551. master->setup = spi_bitbang_setup;
  552. master->cleanup = spi_bitbang_cleanup;
  553. p->bitbang.master = master;
  554. p->bitbang.chipselect = sh_msiof_spi_chipselect;
  555. p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
  556. p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
  557. p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
  558. p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
  559. p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
  560. p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
  561. ret = spi_bitbang_start(&p->bitbang);
  562. if (ret == 0)
  563. return 0;
  564. pm_runtime_disable(&pdev->dev);
  565. err3:
  566. iounmap(p->mapbase);
  567. err2:
  568. clk_put(p->clk);
  569. err1:
  570. spi_master_put(master);
  571. err0:
  572. return ret;
  573. }
  574. static int sh_msiof_spi_remove(struct platform_device *pdev)
  575. {
  576. struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
  577. int ret;
  578. ret = spi_bitbang_stop(&p->bitbang);
  579. if (!ret) {
  580. pm_runtime_disable(&pdev->dev);
  581. free_irq(platform_get_irq(pdev, 0), p);
  582. iounmap(p->mapbase);
  583. clk_put(p->clk);
  584. spi_master_put(p->bitbang.master);
  585. }
  586. return ret;
  587. }
  588. static int sh_msiof_spi_runtime_nop(struct device *dev)
  589. {
  590. /* Runtime PM callback shared between ->runtime_suspend()
  591. * and ->runtime_resume(). Simply returns success.
  592. *
  593. * This driver re-initializes all registers after
  594. * pm_runtime_get_sync() anyway so there is no need
  595. * to save and restore registers here.
  596. */
  597. return 0;
  598. }
  599. static struct dev_pm_ops sh_msiof_spi_dev_pm_ops = {
  600. .runtime_suspend = sh_msiof_spi_runtime_nop,
  601. .runtime_resume = sh_msiof_spi_runtime_nop,
  602. };
  603. static struct platform_driver sh_msiof_spi_drv = {
  604. .probe = sh_msiof_spi_probe,
  605. .remove = sh_msiof_spi_remove,
  606. .driver = {
  607. .name = "spi_sh_msiof",
  608. .owner = THIS_MODULE,
  609. .pm = &sh_msiof_spi_dev_pm_ops,
  610. },
  611. };
  612. static int __init sh_msiof_spi_init(void)
  613. {
  614. return platform_driver_register(&sh_msiof_spi_drv);
  615. }
  616. module_init(sh_msiof_spi_init);
  617. static void __exit sh_msiof_spi_exit(void)
  618. {
  619. platform_driver_unregister(&sh_msiof_spi_drv);
  620. }
  621. module_exit(sh_msiof_spi_exit);
  622. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  623. MODULE_AUTHOR("Magnus Damm");
  624. MODULE_LICENSE("GPL v2");
  625. MODULE_ALIAS("platform:spi_sh_msiof");