spi-omap2-mcspi.c 35 KB

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