spi-omap2-mcspi.c 38 KB

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