spi-omap2-mcspi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/omap-dma.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/gcd.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/gpio.h>
  37. #include <linux/platform_data/spi-omap2-mcspi.h>
  38. #define OMAP2_MCSPI_MAX_FREQ 48000000
  39. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  40. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  41. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  42. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  43. #define OMAP2_MCSPI_REVISION 0x00
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  51. /* per-channel banks, 0x14 bytes each, first is: */
  52. #define OMAP2_MCSPI_CHCONF0 0x2c
  53. #define OMAP2_MCSPI_CHSTAT0 0x30
  54. #define OMAP2_MCSPI_CHCTRL0 0x34
  55. #define OMAP2_MCSPI_TX0 0x38
  56. #define OMAP2_MCSPI_RX0 0x3c
  57. /* per-register bitmasks: */
  58. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  59. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  60. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  61. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  62. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  63. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  64. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  65. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  66. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  67. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  68. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  69. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  70. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  71. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  72. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  73. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  74. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  75. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  76. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  77. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  78. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  79. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  80. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  81. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  82. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  83. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  84. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  85. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  86. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  87. /* We have 2 DMA channels per CS, one for RX and one for TX */
  88. struct omap2_mcspi_dma {
  89. struct dma_chan *dma_tx;
  90. struct dma_chan *dma_rx;
  91. int dma_tx_sync_dev;
  92. int dma_rx_sync_dev;
  93. struct completion dma_tx_completion;
  94. struct completion dma_rx_completion;
  95. char dma_rx_ch_name[14];
  96. char dma_tx_ch_name[14];
  97. };
  98. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  99. * cache operations; better heuristics consider wordsize and bitrate.
  100. */
  101. #define DMA_MIN_BYTES 160
  102. /*
  103. * Used for context save and restore, structure members to be updated whenever
  104. * corresponding registers are modified.
  105. */
  106. struct omap2_mcspi_regs {
  107. u32 modulctrl;
  108. u32 wakeupenable;
  109. struct list_head cs;
  110. };
  111. struct omap2_mcspi {
  112. struct spi_master *master;
  113. /* Virtual base address of the controller */
  114. void __iomem *base;
  115. unsigned long phys;
  116. /* SPI1 has 4 channels, while SPI2 has 2 */
  117. struct omap2_mcspi_dma *dma_channels;
  118. struct device *dev;
  119. struct omap2_mcspi_regs ctx;
  120. int fifo_depth;
  121. unsigned int pin_dir:1;
  122. };
  123. struct omap2_mcspi_cs {
  124. void __iomem *base;
  125. unsigned long phys;
  126. int word_len;
  127. u16 mode;
  128. struct list_head node;
  129. /* Context save and restore shadow register */
  130. u32 chconf0, chctrl0;
  131. };
  132. static inline void mcspi_write_reg(struct spi_master *master,
  133. int idx, u32 val)
  134. {
  135. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  136. writel_relaxed(val, mcspi->base + idx);
  137. }
  138. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  139. {
  140. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  141. return readl_relaxed(mcspi->base + idx);
  142. }
  143. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  144. int idx, u32 val)
  145. {
  146. struct omap2_mcspi_cs *cs = spi->controller_state;
  147. writel_relaxed(val, cs->base + idx);
  148. }
  149. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. return readl_relaxed(cs->base + idx);
  153. }
  154. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. return cs->chconf0;
  158. }
  159. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  160. {
  161. struct omap2_mcspi_cs *cs = spi->controller_state;
  162. cs->chconf0 = val;
  163. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  164. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  165. }
  166. static inline int mcspi_bytes_per_word(int word_len)
  167. {
  168. if (word_len <= 8)
  169. return 1;
  170. else if (word_len <= 16)
  171. return 2;
  172. else /* word_len <= 32 */
  173. return 4;
  174. }
  175. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  176. int is_read, int enable)
  177. {
  178. u32 l, rw;
  179. l = mcspi_cached_chconf0(spi);
  180. if (is_read) /* 1 is read, 0 write */
  181. rw = OMAP2_MCSPI_CHCONF_DMAR;
  182. else
  183. rw = OMAP2_MCSPI_CHCONF_DMAW;
  184. if (enable)
  185. l |= rw;
  186. else
  187. l &= ~rw;
  188. mcspi_write_chconf0(spi, l);
  189. }
  190. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  191. {
  192. struct omap2_mcspi_cs *cs = spi->controller_state;
  193. u32 l;
  194. l = cs->chctrl0;
  195. if (enable)
  196. l |= OMAP2_MCSPI_CHCTRL_EN;
  197. else
  198. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  199. cs->chctrl0 = l;
  200. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  201. /* Flash post-writes */
  202. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  203. }
  204. static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
  205. {
  206. u32 l;
  207. /* The controller handles the inverted chip selects
  208. * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
  209. * the inversion from the core spi_set_cs function.
  210. */
  211. if (spi->mode & SPI_CS_HIGH)
  212. enable = !enable;
  213. if (spi->controller_state) {
  214. l = mcspi_cached_chconf0(spi);
  215. if (enable)
  216. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  217. else
  218. l |= OMAP2_MCSPI_CHCONF_FORCE;
  219. mcspi_write_chconf0(spi, l);
  220. }
  221. }
  222. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  223. {
  224. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  225. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  226. u32 l;
  227. /*
  228. * Setup when switching from (reset default) slave mode
  229. * to single-channel master mode
  230. */
  231. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  232. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
  233. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  234. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  235. ctx->modulctrl = l;
  236. }
  237. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  238. struct spi_transfer *t, int enable)
  239. {
  240. struct spi_master *master = spi->master;
  241. struct omap2_mcspi_cs *cs = spi->controller_state;
  242. struct omap2_mcspi *mcspi;
  243. unsigned int wcnt;
  244. int max_fifo_depth, fifo_depth, bytes_per_word;
  245. u32 chconf, xferlevel;
  246. mcspi = spi_master_get_devdata(master);
  247. chconf = mcspi_cached_chconf0(spi);
  248. if (enable) {
  249. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  250. if (t->len % bytes_per_word != 0)
  251. goto disable_fifo;
  252. if (t->rx_buf != NULL && t->tx_buf != NULL)
  253. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  254. else
  255. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  256. fifo_depth = gcd(t->len, max_fifo_depth);
  257. if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
  258. goto disable_fifo;
  259. wcnt = t->len / bytes_per_word;
  260. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  261. goto disable_fifo;
  262. xferlevel = wcnt << 16;
  263. if (t->rx_buf != NULL) {
  264. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  265. xferlevel |= (fifo_depth - 1) << 8;
  266. }
  267. if (t->tx_buf != NULL) {
  268. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  269. xferlevel |= fifo_depth - 1;
  270. }
  271. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  272. mcspi_write_chconf0(spi, chconf);
  273. mcspi->fifo_depth = fifo_depth;
  274. return;
  275. }
  276. disable_fifo:
  277. if (t->rx_buf != NULL)
  278. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  279. if (t->tx_buf != NULL)
  280. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  281. mcspi_write_chconf0(spi, chconf);
  282. mcspi->fifo_depth = 0;
  283. }
  284. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  285. {
  286. struct spi_master *spi_cntrl = mcspi->master;
  287. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  288. struct omap2_mcspi_cs *cs;
  289. /* McSPI: context restore */
  290. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  291. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  292. list_for_each_entry(cs, &ctx->cs, node)
  293. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  294. }
  295. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  296. {
  297. unsigned long timeout;
  298. timeout = jiffies + msecs_to_jiffies(1000);
  299. while (!(readl_relaxed(reg) & bit)) {
  300. if (time_after(jiffies, timeout)) {
  301. if (!(readl_relaxed(reg) & bit))
  302. return -ETIMEDOUT;
  303. else
  304. return 0;
  305. }
  306. cpu_relax();
  307. }
  308. return 0;
  309. }
  310. static void omap2_mcspi_rx_callback(void *data)
  311. {
  312. struct spi_device *spi = data;
  313. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  314. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  315. /* We must disable the DMA RX request */
  316. omap2_mcspi_set_dma_req(spi, 1, 0);
  317. complete(&mcspi_dma->dma_rx_completion);
  318. }
  319. static void omap2_mcspi_tx_callback(void *data)
  320. {
  321. struct spi_device *spi = data;
  322. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  323. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  324. /* We must disable the DMA TX request */
  325. omap2_mcspi_set_dma_req(spi, 0, 0);
  326. complete(&mcspi_dma->dma_tx_completion);
  327. }
  328. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  329. struct spi_transfer *xfer,
  330. struct dma_slave_config cfg)
  331. {
  332. struct omap2_mcspi *mcspi;
  333. struct omap2_mcspi_dma *mcspi_dma;
  334. unsigned int count;
  335. mcspi = spi_master_get_devdata(spi->master);
  336. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  337. count = xfer->len;
  338. if (mcspi_dma->dma_tx) {
  339. struct dma_async_tx_descriptor *tx;
  340. struct scatterlist sg;
  341. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  342. sg_init_table(&sg, 1);
  343. sg_dma_address(&sg) = xfer->tx_dma;
  344. sg_dma_len(&sg) = xfer->len;
  345. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
  346. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  347. if (tx) {
  348. tx->callback = omap2_mcspi_tx_callback;
  349. tx->callback_param = spi;
  350. dmaengine_submit(tx);
  351. } else {
  352. /* FIXME: fall back to PIO? */
  353. }
  354. }
  355. dma_async_issue_pending(mcspi_dma->dma_tx);
  356. omap2_mcspi_set_dma_req(spi, 0, 1);
  357. }
  358. static unsigned
  359. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  360. struct dma_slave_config cfg,
  361. unsigned es)
  362. {
  363. struct omap2_mcspi *mcspi;
  364. struct omap2_mcspi_dma *mcspi_dma;
  365. unsigned int count, dma_count;
  366. u32 l;
  367. int elements = 0;
  368. int word_len, element_count;
  369. struct omap2_mcspi_cs *cs = spi->controller_state;
  370. mcspi = spi_master_get_devdata(spi->master);
  371. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  372. count = xfer->len;
  373. dma_count = xfer->len;
  374. if (mcspi->fifo_depth == 0)
  375. dma_count -= es;
  376. word_len = cs->word_len;
  377. l = mcspi_cached_chconf0(spi);
  378. if (word_len <= 8)
  379. element_count = count;
  380. else if (word_len <= 16)
  381. element_count = count >> 1;
  382. else /* word_len <= 32 */
  383. element_count = count >> 2;
  384. if (mcspi_dma->dma_rx) {
  385. struct dma_async_tx_descriptor *tx;
  386. struct scatterlist sg;
  387. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  388. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  389. dma_count -= es;
  390. sg_init_table(&sg, 1);
  391. sg_dma_address(&sg) = xfer->rx_dma;
  392. sg_dma_len(&sg) = dma_count;
  393. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
  394. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
  395. DMA_CTRL_ACK);
  396. if (tx) {
  397. tx->callback = omap2_mcspi_rx_callback;
  398. tx->callback_param = spi;
  399. dmaengine_submit(tx);
  400. } else {
  401. /* FIXME: fall back to PIO? */
  402. }
  403. }
  404. dma_async_issue_pending(mcspi_dma->dma_rx);
  405. omap2_mcspi_set_dma_req(spi, 1, 1);
  406. wait_for_completion(&mcspi_dma->dma_rx_completion);
  407. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  408. DMA_FROM_DEVICE);
  409. if (mcspi->fifo_depth > 0)
  410. return count;
  411. omap2_mcspi_set_enable(spi, 0);
  412. elements = element_count - 1;
  413. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  414. elements--;
  415. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  416. & OMAP2_MCSPI_CHSTAT_RXS)) {
  417. u32 w;
  418. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  419. if (word_len <= 8)
  420. ((u8 *)xfer->rx_buf)[elements++] = w;
  421. else if (word_len <= 16)
  422. ((u16 *)xfer->rx_buf)[elements++] = w;
  423. else /* word_len <= 32 */
  424. ((u32 *)xfer->rx_buf)[elements++] = w;
  425. } else {
  426. int bytes_per_word = mcspi_bytes_per_word(word_len);
  427. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  428. count -= (bytes_per_word << 1);
  429. omap2_mcspi_set_enable(spi, 1);
  430. return count;
  431. }
  432. }
  433. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  434. & OMAP2_MCSPI_CHSTAT_RXS)) {
  435. u32 w;
  436. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  437. if (word_len <= 8)
  438. ((u8 *)xfer->rx_buf)[elements] = w;
  439. else if (word_len <= 16)
  440. ((u16 *)xfer->rx_buf)[elements] = w;
  441. else /* word_len <= 32 */
  442. ((u32 *)xfer->rx_buf)[elements] = w;
  443. } else {
  444. dev_err(&spi->dev, "DMA RX last word empty\n");
  445. count -= mcspi_bytes_per_word(word_len);
  446. }
  447. omap2_mcspi_set_enable(spi, 1);
  448. return count;
  449. }
  450. static unsigned
  451. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  452. {
  453. struct omap2_mcspi *mcspi;
  454. struct omap2_mcspi_cs *cs = spi->controller_state;
  455. struct omap2_mcspi_dma *mcspi_dma;
  456. unsigned int count;
  457. u32 l;
  458. u8 *rx;
  459. const u8 *tx;
  460. struct dma_slave_config cfg;
  461. enum dma_slave_buswidth width;
  462. unsigned es;
  463. u32 burst;
  464. void __iomem *chstat_reg;
  465. void __iomem *irqstat_reg;
  466. int wait_res;
  467. mcspi = spi_master_get_devdata(spi->master);
  468. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  469. l = mcspi_cached_chconf0(spi);
  470. if (cs->word_len <= 8) {
  471. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  472. es = 1;
  473. } else if (cs->word_len <= 16) {
  474. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  475. es = 2;
  476. } else {
  477. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  478. es = 4;
  479. }
  480. count = xfer->len;
  481. burst = 1;
  482. if (mcspi->fifo_depth > 0) {
  483. if (count > mcspi->fifo_depth)
  484. burst = mcspi->fifo_depth / es;
  485. else
  486. burst = count / es;
  487. }
  488. memset(&cfg, 0, sizeof(cfg));
  489. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  490. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  491. cfg.src_addr_width = width;
  492. cfg.dst_addr_width = width;
  493. cfg.src_maxburst = burst;
  494. cfg.dst_maxburst = burst;
  495. rx = xfer->rx_buf;
  496. tx = xfer->tx_buf;
  497. if (tx != NULL)
  498. omap2_mcspi_tx_dma(spi, xfer, cfg);
  499. if (rx != NULL)
  500. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  501. if (tx != NULL) {
  502. wait_for_completion(&mcspi_dma->dma_tx_completion);
  503. dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
  504. DMA_TO_DEVICE);
  505. if (mcspi->fifo_depth > 0) {
  506. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  507. if (mcspi_wait_for_reg_bit(irqstat_reg,
  508. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  509. dev_err(&spi->dev, "EOW timed out\n");
  510. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  511. OMAP2_MCSPI_IRQSTATUS_EOW);
  512. }
  513. /* for TX_ONLY mode, be sure all words have shifted out */
  514. if (rx == NULL) {
  515. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  516. if (mcspi->fifo_depth > 0) {
  517. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  518. OMAP2_MCSPI_CHSTAT_TXFFE);
  519. if (wait_res < 0)
  520. dev_err(&spi->dev, "TXFFE timed out\n");
  521. } else {
  522. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  523. OMAP2_MCSPI_CHSTAT_TXS);
  524. if (wait_res < 0)
  525. dev_err(&spi->dev, "TXS timed out\n");
  526. }
  527. if (wait_res >= 0 &&
  528. (mcspi_wait_for_reg_bit(chstat_reg,
  529. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  530. dev_err(&spi->dev, "EOT timed out\n");
  531. }
  532. }
  533. return count;
  534. }
  535. static unsigned
  536. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  537. {
  538. struct omap2_mcspi *mcspi;
  539. struct omap2_mcspi_cs *cs = spi->controller_state;
  540. unsigned int count, c;
  541. u32 l;
  542. void __iomem *base = cs->base;
  543. void __iomem *tx_reg;
  544. void __iomem *rx_reg;
  545. void __iomem *chstat_reg;
  546. int word_len;
  547. mcspi = spi_master_get_devdata(spi->master);
  548. count = xfer->len;
  549. c = count;
  550. word_len = cs->word_len;
  551. l = mcspi_cached_chconf0(spi);
  552. /* We store the pre-calculated register addresses on stack to speed
  553. * up the transfer loop. */
  554. tx_reg = base + OMAP2_MCSPI_TX0;
  555. rx_reg = base + OMAP2_MCSPI_RX0;
  556. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  557. if (c < (word_len>>3))
  558. return 0;
  559. if (word_len <= 8) {
  560. u8 *rx;
  561. const u8 *tx;
  562. rx = xfer->rx_buf;
  563. tx = xfer->tx_buf;
  564. do {
  565. c -= 1;
  566. if (tx != NULL) {
  567. if (mcspi_wait_for_reg_bit(chstat_reg,
  568. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  569. dev_err(&spi->dev, "TXS timed out\n");
  570. goto out;
  571. }
  572. dev_vdbg(&spi->dev, "write-%d %02x\n",
  573. word_len, *tx);
  574. writel_relaxed(*tx++, tx_reg);
  575. }
  576. if (rx != NULL) {
  577. if (mcspi_wait_for_reg_bit(chstat_reg,
  578. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  579. dev_err(&spi->dev, "RXS timed out\n");
  580. goto out;
  581. }
  582. if (c == 1 && tx == NULL &&
  583. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  584. omap2_mcspi_set_enable(spi, 0);
  585. *rx++ = readl_relaxed(rx_reg);
  586. dev_vdbg(&spi->dev, "read-%d %02x\n",
  587. word_len, *(rx - 1));
  588. if (mcspi_wait_for_reg_bit(chstat_reg,
  589. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  590. dev_err(&spi->dev,
  591. "RXS timed out\n");
  592. goto out;
  593. }
  594. c = 0;
  595. } else if (c == 0 && tx == NULL) {
  596. omap2_mcspi_set_enable(spi, 0);
  597. }
  598. *rx++ = readl_relaxed(rx_reg);
  599. dev_vdbg(&spi->dev, "read-%d %02x\n",
  600. word_len, *(rx - 1));
  601. }
  602. } while (c);
  603. } else if (word_len <= 16) {
  604. u16 *rx;
  605. const u16 *tx;
  606. rx = xfer->rx_buf;
  607. tx = xfer->tx_buf;
  608. do {
  609. c -= 2;
  610. if (tx != NULL) {
  611. if (mcspi_wait_for_reg_bit(chstat_reg,
  612. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  613. dev_err(&spi->dev, "TXS timed out\n");
  614. goto out;
  615. }
  616. dev_vdbg(&spi->dev, "write-%d %04x\n",
  617. word_len, *tx);
  618. writel_relaxed(*tx++, tx_reg);
  619. }
  620. if (rx != NULL) {
  621. if (mcspi_wait_for_reg_bit(chstat_reg,
  622. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  623. dev_err(&spi->dev, "RXS timed out\n");
  624. goto out;
  625. }
  626. if (c == 2 && tx == NULL &&
  627. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  628. omap2_mcspi_set_enable(spi, 0);
  629. *rx++ = readl_relaxed(rx_reg);
  630. dev_vdbg(&spi->dev, "read-%d %04x\n",
  631. word_len, *(rx - 1));
  632. if (mcspi_wait_for_reg_bit(chstat_reg,
  633. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  634. dev_err(&spi->dev,
  635. "RXS timed out\n");
  636. goto out;
  637. }
  638. c = 0;
  639. } else if (c == 0 && tx == NULL) {
  640. omap2_mcspi_set_enable(spi, 0);
  641. }
  642. *rx++ = readl_relaxed(rx_reg);
  643. dev_vdbg(&spi->dev, "read-%d %04x\n",
  644. word_len, *(rx - 1));
  645. }
  646. } while (c >= 2);
  647. } else if (word_len <= 32) {
  648. u32 *rx;
  649. const u32 *tx;
  650. rx = xfer->rx_buf;
  651. tx = xfer->tx_buf;
  652. do {
  653. c -= 4;
  654. if (tx != NULL) {
  655. if (mcspi_wait_for_reg_bit(chstat_reg,
  656. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  657. dev_err(&spi->dev, "TXS timed out\n");
  658. goto out;
  659. }
  660. dev_vdbg(&spi->dev, "write-%d %08x\n",
  661. word_len, *tx);
  662. writel_relaxed(*tx++, tx_reg);
  663. }
  664. if (rx != NULL) {
  665. if (mcspi_wait_for_reg_bit(chstat_reg,
  666. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  667. dev_err(&spi->dev, "RXS timed out\n");
  668. goto out;
  669. }
  670. if (c == 4 && tx == NULL &&
  671. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  672. omap2_mcspi_set_enable(spi, 0);
  673. *rx++ = readl_relaxed(rx_reg);
  674. dev_vdbg(&spi->dev, "read-%d %08x\n",
  675. word_len, *(rx - 1));
  676. if (mcspi_wait_for_reg_bit(chstat_reg,
  677. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  678. dev_err(&spi->dev,
  679. "RXS timed out\n");
  680. goto out;
  681. }
  682. c = 0;
  683. } else if (c == 0 && tx == NULL) {
  684. omap2_mcspi_set_enable(spi, 0);
  685. }
  686. *rx++ = readl_relaxed(rx_reg);
  687. dev_vdbg(&spi->dev, "read-%d %08x\n",
  688. word_len, *(rx - 1));
  689. }
  690. } while (c >= 4);
  691. }
  692. /* for TX_ONLY mode, be sure all words have shifted out */
  693. if (xfer->rx_buf == NULL) {
  694. if (mcspi_wait_for_reg_bit(chstat_reg,
  695. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  696. dev_err(&spi->dev, "TXS timed out\n");
  697. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  698. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  699. dev_err(&spi->dev, "EOT timed out\n");
  700. /* disable chan to purge rx datas received in TX_ONLY transfer,
  701. * otherwise these rx datas will affect the direct following
  702. * RX_ONLY transfer.
  703. */
  704. omap2_mcspi_set_enable(spi, 0);
  705. }
  706. out:
  707. omap2_mcspi_set_enable(spi, 1);
  708. return count - c;
  709. }
  710. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  711. {
  712. u32 div;
  713. for (div = 0; div < 15; div++)
  714. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  715. return div;
  716. return 15;
  717. }
  718. /* called only when no transfer is active to this device */
  719. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  720. struct spi_transfer *t)
  721. {
  722. struct omap2_mcspi_cs *cs = spi->controller_state;
  723. struct omap2_mcspi *mcspi;
  724. struct spi_master *spi_cntrl;
  725. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  726. u8 word_len = spi->bits_per_word;
  727. u32 speed_hz = spi->max_speed_hz;
  728. mcspi = spi_master_get_devdata(spi->master);
  729. spi_cntrl = mcspi->master;
  730. if (t != NULL && t->bits_per_word)
  731. word_len = t->bits_per_word;
  732. cs->word_len = word_len;
  733. if (t && t->speed_hz)
  734. speed_hz = t->speed_hz;
  735. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  736. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  737. clkd = omap2_mcspi_calc_divisor(speed_hz);
  738. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  739. clkg = 0;
  740. } else {
  741. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  742. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  743. clkd = (div - 1) & 0xf;
  744. extclk = (div - 1) >> 4;
  745. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  746. }
  747. l = mcspi_cached_chconf0(spi);
  748. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  749. * REVISIT: this controller could support SPI_3WIRE mode.
  750. */
  751. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  752. l &= ~OMAP2_MCSPI_CHCONF_IS;
  753. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  754. l |= OMAP2_MCSPI_CHCONF_DPE0;
  755. } else {
  756. l |= OMAP2_MCSPI_CHCONF_IS;
  757. l |= OMAP2_MCSPI_CHCONF_DPE1;
  758. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  759. }
  760. /* wordlength */
  761. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  762. l |= (word_len - 1) << 7;
  763. /* set chipselect polarity; manage with FORCE */
  764. if (!(spi->mode & SPI_CS_HIGH))
  765. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  766. else
  767. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  768. /* set clock divisor */
  769. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  770. l |= clkd << 2;
  771. /* set clock granularity */
  772. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  773. l |= clkg;
  774. if (clkg) {
  775. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  776. cs->chctrl0 |= extclk << 8;
  777. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  778. }
  779. /* set SPI mode 0..3 */
  780. if (spi->mode & SPI_CPOL)
  781. l |= OMAP2_MCSPI_CHCONF_POL;
  782. else
  783. l &= ~OMAP2_MCSPI_CHCONF_POL;
  784. if (spi->mode & SPI_CPHA)
  785. l |= OMAP2_MCSPI_CHCONF_PHA;
  786. else
  787. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  788. mcspi_write_chconf0(spi, l);
  789. cs->mode = spi->mode;
  790. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  791. speed_hz,
  792. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  793. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  794. return 0;
  795. }
  796. /*
  797. * Note that we currently allow DMA only if we get a channel
  798. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  799. */
  800. static int omap2_mcspi_request_dma(struct spi_device *spi)
  801. {
  802. struct spi_master *master = spi->master;
  803. struct omap2_mcspi *mcspi;
  804. struct omap2_mcspi_dma *mcspi_dma;
  805. dma_cap_mask_t mask;
  806. unsigned sig;
  807. mcspi = spi_master_get_devdata(master);
  808. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  809. init_completion(&mcspi_dma->dma_rx_completion);
  810. init_completion(&mcspi_dma->dma_tx_completion);
  811. dma_cap_zero(mask);
  812. dma_cap_set(DMA_SLAVE, mask);
  813. sig = mcspi_dma->dma_rx_sync_dev;
  814. mcspi_dma->dma_rx =
  815. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  816. &sig, &master->dev,
  817. mcspi_dma->dma_rx_ch_name);
  818. if (!mcspi_dma->dma_rx)
  819. goto no_dma;
  820. sig = mcspi_dma->dma_tx_sync_dev;
  821. mcspi_dma->dma_tx =
  822. dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
  823. &sig, &master->dev,
  824. mcspi_dma->dma_tx_ch_name);
  825. if (!mcspi_dma->dma_tx) {
  826. dma_release_channel(mcspi_dma->dma_rx);
  827. mcspi_dma->dma_rx = NULL;
  828. goto no_dma;
  829. }
  830. return 0;
  831. no_dma:
  832. dev_warn(&spi->dev, "not using DMA for McSPI\n");
  833. return -EAGAIN;
  834. }
  835. static int omap2_mcspi_setup(struct spi_device *spi)
  836. {
  837. int ret;
  838. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  839. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  840. struct omap2_mcspi_dma *mcspi_dma;
  841. struct omap2_mcspi_cs *cs = spi->controller_state;
  842. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  843. if (!cs) {
  844. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  845. if (!cs)
  846. return -ENOMEM;
  847. cs->base = mcspi->base + spi->chip_select * 0x14;
  848. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  849. cs->mode = 0;
  850. cs->chconf0 = 0;
  851. cs->chctrl0 = 0;
  852. spi->controller_state = cs;
  853. /* Link this to context save list */
  854. list_add_tail(&cs->node, &ctx->cs);
  855. }
  856. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  857. ret = omap2_mcspi_request_dma(spi);
  858. if (ret < 0 && ret != -EAGAIN)
  859. return ret;
  860. }
  861. if (gpio_is_valid(spi->cs_gpio)) {
  862. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  863. if (ret) {
  864. dev_err(&spi->dev, "failed to request gpio\n");
  865. return ret;
  866. }
  867. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  868. }
  869. ret = pm_runtime_get_sync(mcspi->dev);
  870. if (ret < 0)
  871. return ret;
  872. ret = omap2_mcspi_setup_transfer(spi, NULL);
  873. pm_runtime_mark_last_busy(mcspi->dev);
  874. pm_runtime_put_autosuspend(mcspi->dev);
  875. return ret;
  876. }
  877. static void omap2_mcspi_cleanup(struct spi_device *spi)
  878. {
  879. struct omap2_mcspi *mcspi;
  880. struct omap2_mcspi_dma *mcspi_dma;
  881. struct omap2_mcspi_cs *cs;
  882. mcspi = spi_master_get_devdata(spi->master);
  883. if (spi->controller_state) {
  884. /* Unlink controller state from context save list */
  885. cs = spi->controller_state;
  886. list_del(&cs->node);
  887. kfree(cs);
  888. }
  889. if (spi->chip_select < spi->master->num_chipselect) {
  890. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  891. if (mcspi_dma->dma_rx) {
  892. dma_release_channel(mcspi_dma->dma_rx);
  893. mcspi_dma->dma_rx = NULL;
  894. }
  895. if (mcspi_dma->dma_tx) {
  896. dma_release_channel(mcspi_dma->dma_tx);
  897. mcspi_dma->dma_tx = NULL;
  898. }
  899. }
  900. if (gpio_is_valid(spi->cs_gpio))
  901. gpio_free(spi->cs_gpio);
  902. }
  903. static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
  904. struct spi_device *spi, struct spi_transfer *t)
  905. {
  906. /* We only enable one channel at a time -- the one whose message is
  907. * -- although this controller would gladly
  908. * arbitrate among multiple channels. This corresponds to "single
  909. * channel" master mode. As a side effect, we need to manage the
  910. * chipselect with the FORCE bit ... CS != channel enable.
  911. */
  912. struct spi_master *master;
  913. struct omap2_mcspi_dma *mcspi_dma;
  914. struct omap2_mcspi_cs *cs;
  915. struct omap2_mcspi_device_config *cd;
  916. int par_override = 0;
  917. int status = 0;
  918. u32 chconf;
  919. master = spi->master;
  920. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  921. cs = spi->controller_state;
  922. cd = spi->controller_data;
  923. /*
  924. * The slave driver could have changed spi->mode in which case
  925. * it will be different from cs->mode (the current hardware setup).
  926. * If so, set par_override (even though its not a parity issue) so
  927. * omap2_mcspi_setup_transfer will be called to configure the hardware
  928. * with the correct mode on the first iteration of the loop below.
  929. */
  930. if (spi->mode != cs->mode)
  931. par_override = 1;
  932. omap2_mcspi_set_enable(spi, 0);
  933. if (gpio_is_valid(spi->cs_gpio))
  934. omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
  935. if (par_override ||
  936. (t->speed_hz != spi->max_speed_hz) ||
  937. (t->bits_per_word != spi->bits_per_word)) {
  938. par_override = 1;
  939. status = omap2_mcspi_setup_transfer(spi, t);
  940. if (status < 0)
  941. goto out;
  942. if (t->speed_hz == spi->max_speed_hz &&
  943. t->bits_per_word == spi->bits_per_word)
  944. par_override = 0;
  945. }
  946. if (cd && cd->cs_per_word) {
  947. chconf = mcspi->ctx.modulctrl;
  948. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  949. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  950. mcspi->ctx.modulctrl =
  951. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  952. }
  953. chconf = mcspi_cached_chconf0(spi);
  954. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  955. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  956. if (t->tx_buf == NULL)
  957. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  958. else if (t->rx_buf == NULL)
  959. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  960. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  961. /* Turbo mode is for more than one word */
  962. if (t->len > ((cs->word_len + 7) >> 3))
  963. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  964. }
  965. mcspi_write_chconf0(spi, chconf);
  966. if (t->len) {
  967. unsigned count;
  968. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  969. (t->len >= DMA_MIN_BYTES))
  970. omap2_mcspi_set_fifo(spi, t, 1);
  971. omap2_mcspi_set_enable(spi, 1);
  972. /* RX_ONLY mode needs dummy data in TX reg */
  973. if (t->tx_buf == NULL)
  974. writel_relaxed(0, cs->base
  975. + OMAP2_MCSPI_TX0);
  976. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  977. (t->len >= DMA_MIN_BYTES))
  978. count = omap2_mcspi_txrx_dma(spi, t);
  979. else
  980. count = omap2_mcspi_txrx_pio(spi, t);
  981. if (count != t->len) {
  982. status = -EIO;
  983. goto out;
  984. }
  985. }
  986. omap2_mcspi_set_enable(spi, 0);
  987. if (mcspi->fifo_depth > 0)
  988. omap2_mcspi_set_fifo(spi, t, 0);
  989. out:
  990. /* Restore defaults if they were overriden */
  991. if (par_override) {
  992. par_override = 0;
  993. status = omap2_mcspi_setup_transfer(spi, NULL);
  994. }
  995. if (cd && cd->cs_per_word) {
  996. chconf = mcspi->ctx.modulctrl;
  997. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  998. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  999. mcspi->ctx.modulctrl =
  1000. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1001. }
  1002. omap2_mcspi_set_enable(spi, 0);
  1003. if (gpio_is_valid(spi->cs_gpio))
  1004. omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
  1005. if (mcspi->fifo_depth > 0 && t)
  1006. omap2_mcspi_set_fifo(spi, t, 0);
  1007. return status;
  1008. }
  1009. static int omap2_mcspi_transfer_one(struct spi_master *master,
  1010. struct spi_device *spi, struct spi_transfer *t)
  1011. {
  1012. struct omap2_mcspi *mcspi;
  1013. struct omap2_mcspi_dma *mcspi_dma;
  1014. const void *tx_buf = t->tx_buf;
  1015. void *rx_buf = t->rx_buf;
  1016. unsigned len = t->len;
  1017. mcspi = spi_master_get_devdata(master);
  1018. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  1019. if ((len && !(rx_buf || tx_buf))) {
  1020. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  1021. t->speed_hz,
  1022. len,
  1023. tx_buf ? "tx" : "",
  1024. rx_buf ? "rx" : "",
  1025. t->bits_per_word);
  1026. return -EINVAL;
  1027. }
  1028. if (len < DMA_MIN_BYTES)
  1029. goto skip_dma_map;
  1030. if (mcspi_dma->dma_tx && tx_buf != NULL) {
  1031. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  1032. len, DMA_TO_DEVICE);
  1033. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  1034. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  1035. 'T', len);
  1036. return -EINVAL;
  1037. }
  1038. }
  1039. if (mcspi_dma->dma_rx && rx_buf != NULL) {
  1040. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  1041. DMA_FROM_DEVICE);
  1042. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  1043. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  1044. 'R', len);
  1045. if (tx_buf != NULL)
  1046. dma_unmap_single(mcspi->dev, t->tx_dma,
  1047. len, DMA_TO_DEVICE);
  1048. return -EINVAL;
  1049. }
  1050. }
  1051. skip_dma_map:
  1052. return omap2_mcspi_work_one(mcspi, spi, t);
  1053. }
  1054. static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  1055. {
  1056. struct spi_master *master = mcspi->master;
  1057. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1058. int ret = 0;
  1059. ret = pm_runtime_get_sync(mcspi->dev);
  1060. if (ret < 0)
  1061. return ret;
  1062. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1063. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1064. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1065. omap2_mcspi_set_master_mode(master);
  1066. pm_runtime_mark_last_busy(mcspi->dev);
  1067. pm_runtime_put_autosuspend(mcspi->dev);
  1068. return 0;
  1069. }
  1070. static int omap_mcspi_runtime_resume(struct device *dev)
  1071. {
  1072. struct omap2_mcspi *mcspi;
  1073. struct spi_master *master;
  1074. master = dev_get_drvdata(dev);
  1075. mcspi = spi_master_get_devdata(master);
  1076. omap2_mcspi_restore_ctx(mcspi);
  1077. return 0;
  1078. }
  1079. static struct omap2_mcspi_platform_config omap2_pdata = {
  1080. .regs_offset = 0,
  1081. };
  1082. static struct omap2_mcspi_platform_config omap4_pdata = {
  1083. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1084. };
  1085. static const struct of_device_id omap_mcspi_of_match[] = {
  1086. {
  1087. .compatible = "ti,omap2-mcspi",
  1088. .data = &omap2_pdata,
  1089. },
  1090. {
  1091. .compatible = "ti,omap4-mcspi",
  1092. .data = &omap4_pdata,
  1093. },
  1094. { },
  1095. };
  1096. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1097. static int omap2_mcspi_probe(struct platform_device *pdev)
  1098. {
  1099. struct spi_master *master;
  1100. const struct omap2_mcspi_platform_config *pdata;
  1101. struct omap2_mcspi *mcspi;
  1102. struct resource *r;
  1103. int status = 0, i;
  1104. u32 regs_offset = 0;
  1105. static int bus_num = 1;
  1106. struct device_node *node = pdev->dev.of_node;
  1107. const struct of_device_id *match;
  1108. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  1109. if (master == NULL) {
  1110. dev_dbg(&pdev->dev, "master allocation failed\n");
  1111. return -ENOMEM;
  1112. }
  1113. /* the spi->mode bits understood by this driver: */
  1114. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1115. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1116. master->setup = omap2_mcspi_setup;
  1117. master->auto_runtime_pm = true;
  1118. master->transfer_one = omap2_mcspi_transfer_one;
  1119. master->set_cs = omap2_mcspi_set_cs;
  1120. master->cleanup = omap2_mcspi_cleanup;
  1121. master->dev.of_node = node;
  1122. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1123. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1124. platform_set_drvdata(pdev, master);
  1125. mcspi = spi_master_get_devdata(master);
  1126. mcspi->master = master;
  1127. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1128. if (match) {
  1129. u32 num_cs = 1; /* default number of chipselect */
  1130. pdata = match->data;
  1131. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1132. master->num_chipselect = num_cs;
  1133. master->bus_num = bus_num++;
  1134. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1135. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1136. } else {
  1137. pdata = dev_get_platdata(&pdev->dev);
  1138. master->num_chipselect = pdata->num_cs;
  1139. if (pdev->id != -1)
  1140. master->bus_num = pdev->id;
  1141. mcspi->pin_dir = pdata->pin_dir;
  1142. }
  1143. regs_offset = pdata->regs_offset;
  1144. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1145. if (r == NULL) {
  1146. status = -ENODEV;
  1147. goto free_master;
  1148. }
  1149. r->start += regs_offset;
  1150. r->end += regs_offset;
  1151. mcspi->phys = r->start;
  1152. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1153. if (IS_ERR(mcspi->base)) {
  1154. status = PTR_ERR(mcspi->base);
  1155. goto free_master;
  1156. }
  1157. mcspi->dev = &pdev->dev;
  1158. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1159. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1160. sizeof(struct omap2_mcspi_dma),
  1161. GFP_KERNEL);
  1162. if (mcspi->dma_channels == NULL) {
  1163. status = -ENOMEM;
  1164. goto free_master;
  1165. }
  1166. for (i = 0; i < master->num_chipselect; i++) {
  1167. char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
  1168. char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
  1169. struct resource *dma_res;
  1170. sprintf(dma_rx_ch_name, "rx%d", i);
  1171. if (!pdev->dev.of_node) {
  1172. dma_res =
  1173. platform_get_resource_byname(pdev,
  1174. IORESOURCE_DMA,
  1175. dma_rx_ch_name);
  1176. if (!dma_res) {
  1177. dev_dbg(&pdev->dev,
  1178. "cannot get DMA RX channel\n");
  1179. status = -ENODEV;
  1180. break;
  1181. }
  1182. mcspi->dma_channels[i].dma_rx_sync_dev =
  1183. dma_res->start;
  1184. }
  1185. sprintf(dma_tx_ch_name, "tx%d", i);
  1186. if (!pdev->dev.of_node) {
  1187. dma_res =
  1188. platform_get_resource_byname(pdev,
  1189. IORESOURCE_DMA,
  1190. dma_tx_ch_name);
  1191. if (!dma_res) {
  1192. dev_dbg(&pdev->dev,
  1193. "cannot get DMA TX channel\n");
  1194. status = -ENODEV;
  1195. break;
  1196. }
  1197. mcspi->dma_channels[i].dma_tx_sync_dev =
  1198. dma_res->start;
  1199. }
  1200. }
  1201. if (status < 0)
  1202. goto free_master;
  1203. pm_runtime_use_autosuspend(&pdev->dev);
  1204. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1205. pm_runtime_enable(&pdev->dev);
  1206. status = omap2_mcspi_master_setup(mcspi);
  1207. if (status < 0)
  1208. goto disable_pm;
  1209. status = devm_spi_register_master(&pdev->dev, master);
  1210. if (status < 0)
  1211. goto disable_pm;
  1212. return status;
  1213. disable_pm:
  1214. pm_runtime_disable(&pdev->dev);
  1215. free_master:
  1216. spi_master_put(master);
  1217. return status;
  1218. }
  1219. static int omap2_mcspi_remove(struct platform_device *pdev)
  1220. {
  1221. struct spi_master *master = platform_get_drvdata(pdev);
  1222. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1223. pm_runtime_put_sync(mcspi->dev);
  1224. pm_runtime_disable(&pdev->dev);
  1225. return 0;
  1226. }
  1227. /* work with hotplug and coldplug */
  1228. MODULE_ALIAS("platform:omap2_mcspi");
  1229. #ifdef CONFIG_SUSPEND
  1230. /*
  1231. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1232. * unactive state when driver was suspend, then force it to unactive state at
  1233. * wake up.
  1234. */
  1235. static int omap2_mcspi_resume(struct device *dev)
  1236. {
  1237. struct spi_master *master = dev_get_drvdata(dev);
  1238. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1239. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1240. struct omap2_mcspi_cs *cs;
  1241. pm_runtime_get_sync(mcspi->dev);
  1242. list_for_each_entry(cs, &ctx->cs, node) {
  1243. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1244. /*
  1245. * We need to toggle CS state for OMAP take this
  1246. * change in account.
  1247. */
  1248. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1249. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1250. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1251. writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1252. }
  1253. }
  1254. pm_runtime_mark_last_busy(mcspi->dev);
  1255. pm_runtime_put_autosuspend(mcspi->dev);
  1256. return 0;
  1257. }
  1258. #else
  1259. #define omap2_mcspi_resume NULL
  1260. #endif
  1261. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1262. .resume = omap2_mcspi_resume,
  1263. .runtime_resume = omap_mcspi_runtime_resume,
  1264. };
  1265. static struct platform_driver omap2_mcspi_driver = {
  1266. .driver = {
  1267. .name = "omap2_mcspi",
  1268. .pm = &omap2_mcspi_pm_ops,
  1269. .of_match_table = omap_mcspi_of_match,
  1270. },
  1271. .probe = omap2_mcspi_probe,
  1272. .remove = omap2_mcspi_remove,
  1273. };
  1274. module_platform_driver(omap2_mcspi_driver);
  1275. MODULE_LICENSE("GPL");