omap2_mcspi.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/spi/spi.h>
  36. #include <plat/dma.h>
  37. #include <plat/clock.h>
  38. #include <plat/mcspi.h>
  39. #define OMAP2_MCSPI_MAX_FREQ 48000000
  40. /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
  41. #define OMAP2_MCSPI_MAX_CTRL 4
  42. #define OMAP2_MCSPI_REVISION 0x00
  43. #define OMAP2_MCSPI_SYSCONFIG 0x10
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. /* per-channel banks, 0x14 bytes each, first is: */
  51. #define OMAP2_MCSPI_CHCONF0 0x2c
  52. #define OMAP2_MCSPI_CHSTAT0 0x30
  53. #define OMAP2_MCSPI_CHCTRL0 0x34
  54. #define OMAP2_MCSPI_TX0 0x38
  55. #define OMAP2_MCSPI_RX0 0x3c
  56. /* per-register bitmasks: */
  57. #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4)
  58. #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
  59. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
  60. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
  61. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE BIT(0)
  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. int dma_tx_channel;
  88. int dma_rx_channel;
  89. int dma_tx_sync_dev;
  90. int dma_rx_sync_dev;
  91. struct completion dma_tx_completion;
  92. struct completion dma_rx_completion;
  93. };
  94. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  95. * cache operations; better heuristics consider wordsize and bitrate.
  96. */
  97. #define DMA_MIN_BYTES 160
  98. struct omap2_mcspi {
  99. struct work_struct work;
  100. /* lock protects queue and registers */
  101. spinlock_t lock;
  102. struct list_head msg_queue;
  103. struct spi_master *master;
  104. struct clk *ick;
  105. struct clk *fck;
  106. /* Virtual base address of the controller */
  107. void __iomem *base;
  108. unsigned long phys;
  109. /* SPI1 has 4 channels, while SPI2 has 2 */
  110. struct omap2_mcspi_dma *dma_channels;
  111. };
  112. struct omap2_mcspi_cs {
  113. void __iomem *base;
  114. unsigned long phys;
  115. int word_len;
  116. struct list_head node;
  117. /* Context save and restore shadow register */
  118. u32 chconf0;
  119. };
  120. /* used for context save and restore, structure members to be updated whenever
  121. * corresponding registers are modified.
  122. */
  123. struct omap2_mcspi_regs {
  124. u32 sysconfig;
  125. u32 modulctrl;
  126. u32 wakeupenable;
  127. struct list_head cs;
  128. };
  129. static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
  130. static struct workqueue_struct *omap2_mcspi_wq;
  131. #define MOD_REG_BIT(val, mask, set) do { \
  132. if (set) \
  133. val |= mask; \
  134. else \
  135. val &= ~mask; \
  136. } while (0)
  137. static inline void mcspi_write_reg(struct spi_master *master,
  138. int idx, u32 val)
  139. {
  140. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  141. __raw_writel(val, mcspi->base + idx);
  142. }
  143. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  144. {
  145. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  146. return __raw_readl(mcspi->base + idx);
  147. }
  148. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  149. int idx, u32 val)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. __raw_writel(val, cs->base + idx);
  153. }
  154. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. return __raw_readl(cs->base + idx);
  158. }
  159. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  160. {
  161. struct omap2_mcspi_cs *cs = spi->controller_state;
  162. return cs->chconf0;
  163. }
  164. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  165. {
  166. struct omap2_mcspi_cs *cs = spi->controller_state;
  167. cs->chconf0 = val;
  168. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  169. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  170. }
  171. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  172. int is_read, int enable)
  173. {
  174. u32 l, rw;
  175. l = mcspi_cached_chconf0(spi);
  176. if (is_read) /* 1 is read, 0 write */
  177. rw = OMAP2_MCSPI_CHCONF_DMAR;
  178. else
  179. rw = OMAP2_MCSPI_CHCONF_DMAW;
  180. MOD_REG_BIT(l, rw, enable);
  181. mcspi_write_chconf0(spi, l);
  182. }
  183. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  184. {
  185. u32 l;
  186. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  187. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  188. /* Flash post-writes */
  189. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  190. }
  191. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  192. {
  193. u32 l;
  194. l = mcspi_cached_chconf0(spi);
  195. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  196. mcspi_write_chconf0(spi, l);
  197. }
  198. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  199. {
  200. u32 l;
  201. /* setup when switching from (reset default) slave mode
  202. * to single-channel master mode
  203. */
  204. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  205. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  206. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  207. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  208. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  209. omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
  210. }
  211. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  212. {
  213. struct spi_master *spi_cntrl;
  214. struct omap2_mcspi_cs *cs;
  215. spi_cntrl = mcspi->master;
  216. /* McSPI: context restore */
  217. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
  218. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
  219. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG,
  220. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig);
  221. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
  222. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
  223. list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
  224. node)
  225. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  226. }
  227. static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
  228. {
  229. clk_disable(mcspi->ick);
  230. clk_disable(mcspi->fck);
  231. }
  232. static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
  233. {
  234. if (clk_enable(mcspi->ick))
  235. return -ENODEV;
  236. if (clk_enable(mcspi->fck))
  237. return -ENODEV;
  238. omap2_mcspi_restore_ctx(mcspi);
  239. return 0;
  240. }
  241. static unsigned
  242. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  243. {
  244. struct omap2_mcspi *mcspi;
  245. struct omap2_mcspi_cs *cs = spi->controller_state;
  246. struct omap2_mcspi_dma *mcspi_dma;
  247. unsigned int count, c;
  248. unsigned long base, tx_reg, rx_reg;
  249. int word_len, data_type, element_count;
  250. int elements;
  251. u32 l;
  252. u8 * rx;
  253. const u8 * tx;
  254. mcspi = spi_master_get_devdata(spi->master);
  255. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  256. l = mcspi_cached_chconf0(spi);
  257. count = xfer->len;
  258. c = count;
  259. word_len = cs->word_len;
  260. base = cs->phys;
  261. tx_reg = base + OMAP2_MCSPI_TX0;
  262. rx_reg = base + OMAP2_MCSPI_RX0;
  263. rx = xfer->rx_buf;
  264. tx = xfer->tx_buf;
  265. if (word_len <= 8) {
  266. data_type = OMAP_DMA_DATA_TYPE_S8;
  267. element_count = count;
  268. } else if (word_len <= 16) {
  269. data_type = OMAP_DMA_DATA_TYPE_S16;
  270. element_count = count >> 1;
  271. } else /* word_len <= 32 */ {
  272. data_type = OMAP_DMA_DATA_TYPE_S32;
  273. element_count = count >> 2;
  274. }
  275. if (tx != NULL) {
  276. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  277. data_type, element_count, 1,
  278. OMAP_DMA_SYNC_ELEMENT,
  279. mcspi_dma->dma_tx_sync_dev, 0);
  280. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  281. OMAP_DMA_AMODE_CONSTANT,
  282. tx_reg, 0, 0);
  283. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  284. OMAP_DMA_AMODE_POST_INC,
  285. xfer->tx_dma, 0, 0);
  286. }
  287. if (rx != NULL) {
  288. elements = element_count - 1;
  289. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  290. elements--;
  291. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  292. data_type, elements, 1,
  293. OMAP_DMA_SYNC_ELEMENT,
  294. mcspi_dma->dma_rx_sync_dev, 1);
  295. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  296. OMAP_DMA_AMODE_CONSTANT,
  297. rx_reg, 0, 0);
  298. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  299. OMAP_DMA_AMODE_POST_INC,
  300. xfer->rx_dma, 0, 0);
  301. }
  302. if (tx != NULL) {
  303. omap_start_dma(mcspi_dma->dma_tx_channel);
  304. omap2_mcspi_set_dma_req(spi, 0, 1);
  305. }
  306. if (rx != NULL) {
  307. omap_start_dma(mcspi_dma->dma_rx_channel);
  308. omap2_mcspi_set_dma_req(spi, 1, 1);
  309. }
  310. if (tx != NULL) {
  311. wait_for_completion(&mcspi_dma->dma_tx_completion);
  312. dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
  313. }
  314. if (rx != NULL) {
  315. wait_for_completion(&mcspi_dma->dma_rx_completion);
  316. dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
  317. omap2_mcspi_set_enable(spi, 0);
  318. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  319. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  320. & OMAP2_MCSPI_CHSTAT_RXS)) {
  321. u32 w;
  322. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  323. if (word_len <= 8)
  324. ((u8 *)xfer->rx_buf)[elements++] = w;
  325. else if (word_len <= 16)
  326. ((u16 *)xfer->rx_buf)[elements++] = w;
  327. else /* word_len <= 32 */
  328. ((u32 *)xfer->rx_buf)[elements++] = w;
  329. } else {
  330. dev_err(&spi->dev,
  331. "DMA RX penultimate word empty");
  332. count -= (word_len <= 8) ? 2 :
  333. (word_len <= 16) ? 4 :
  334. /* word_len <= 32 */ 8;
  335. omap2_mcspi_set_enable(spi, 1);
  336. return count;
  337. }
  338. }
  339. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  340. & OMAP2_MCSPI_CHSTAT_RXS)) {
  341. u32 w;
  342. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  343. if (word_len <= 8)
  344. ((u8 *)xfer->rx_buf)[elements] = w;
  345. else if (word_len <= 16)
  346. ((u16 *)xfer->rx_buf)[elements] = w;
  347. else /* word_len <= 32 */
  348. ((u32 *)xfer->rx_buf)[elements] = w;
  349. } else {
  350. dev_err(&spi->dev, "DMA RX last word empty");
  351. count -= (word_len <= 8) ? 1 :
  352. (word_len <= 16) ? 2 :
  353. /* word_len <= 32 */ 4;
  354. }
  355. omap2_mcspi_set_enable(spi, 1);
  356. }
  357. return count;
  358. }
  359. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  360. {
  361. unsigned long timeout;
  362. timeout = jiffies + msecs_to_jiffies(1000);
  363. while (!(__raw_readl(reg) & bit)) {
  364. if (time_after(jiffies, timeout))
  365. return -1;
  366. cpu_relax();
  367. }
  368. return 0;
  369. }
  370. static unsigned
  371. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  372. {
  373. struct omap2_mcspi *mcspi;
  374. struct omap2_mcspi_cs *cs = spi->controller_state;
  375. unsigned int count, c;
  376. u32 l;
  377. void __iomem *base = cs->base;
  378. void __iomem *tx_reg;
  379. void __iomem *rx_reg;
  380. void __iomem *chstat_reg;
  381. int word_len;
  382. mcspi = spi_master_get_devdata(spi->master);
  383. count = xfer->len;
  384. c = count;
  385. word_len = cs->word_len;
  386. l = mcspi_cached_chconf0(spi);
  387. /* We store the pre-calculated register addresses on stack to speed
  388. * up the transfer loop. */
  389. tx_reg = base + OMAP2_MCSPI_TX0;
  390. rx_reg = base + OMAP2_MCSPI_RX0;
  391. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  392. if (word_len <= 8) {
  393. u8 *rx;
  394. const u8 *tx;
  395. rx = xfer->rx_buf;
  396. tx = xfer->tx_buf;
  397. do {
  398. c -= 1;
  399. if (tx != NULL) {
  400. if (mcspi_wait_for_reg_bit(chstat_reg,
  401. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  402. dev_err(&spi->dev, "TXS timed out\n");
  403. goto out;
  404. }
  405. #ifdef VERBOSE
  406. dev_dbg(&spi->dev, "write-%d %02x\n",
  407. word_len, *tx);
  408. #endif
  409. __raw_writel(*tx++, tx_reg);
  410. }
  411. if (rx != NULL) {
  412. if (mcspi_wait_for_reg_bit(chstat_reg,
  413. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  414. dev_err(&spi->dev, "RXS timed out\n");
  415. goto out;
  416. }
  417. if (c == 1 && tx == NULL &&
  418. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  419. omap2_mcspi_set_enable(spi, 0);
  420. *rx++ = __raw_readl(rx_reg);
  421. #ifdef VERBOSE
  422. dev_dbg(&spi->dev, "read-%d %02x\n",
  423. word_len, *(rx - 1));
  424. #endif
  425. if (mcspi_wait_for_reg_bit(chstat_reg,
  426. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  427. dev_err(&spi->dev,
  428. "RXS timed out\n");
  429. goto out;
  430. }
  431. c = 0;
  432. } else if (c == 0 && tx == NULL) {
  433. omap2_mcspi_set_enable(spi, 0);
  434. }
  435. *rx++ = __raw_readl(rx_reg);
  436. #ifdef VERBOSE
  437. dev_dbg(&spi->dev, "read-%d %02x\n",
  438. word_len, *(rx - 1));
  439. #endif
  440. }
  441. } while (c);
  442. } else if (word_len <= 16) {
  443. u16 *rx;
  444. const u16 *tx;
  445. rx = xfer->rx_buf;
  446. tx = xfer->tx_buf;
  447. do {
  448. c -= 2;
  449. if (tx != NULL) {
  450. if (mcspi_wait_for_reg_bit(chstat_reg,
  451. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  452. dev_err(&spi->dev, "TXS timed out\n");
  453. goto out;
  454. }
  455. #ifdef VERBOSE
  456. dev_dbg(&spi->dev, "write-%d %04x\n",
  457. word_len, *tx);
  458. #endif
  459. __raw_writel(*tx++, tx_reg);
  460. }
  461. if (rx != NULL) {
  462. if (mcspi_wait_for_reg_bit(chstat_reg,
  463. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  464. dev_err(&spi->dev, "RXS timed out\n");
  465. goto out;
  466. }
  467. if (c == 2 && tx == NULL &&
  468. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  469. omap2_mcspi_set_enable(spi, 0);
  470. *rx++ = __raw_readl(rx_reg);
  471. #ifdef VERBOSE
  472. dev_dbg(&spi->dev, "read-%d %04x\n",
  473. word_len, *(rx - 1));
  474. #endif
  475. if (mcspi_wait_for_reg_bit(chstat_reg,
  476. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  477. dev_err(&spi->dev,
  478. "RXS timed out\n");
  479. goto out;
  480. }
  481. c = 0;
  482. } else if (c == 0 && tx == NULL) {
  483. omap2_mcspi_set_enable(spi, 0);
  484. }
  485. *rx++ = __raw_readl(rx_reg);
  486. #ifdef VERBOSE
  487. dev_dbg(&spi->dev, "read-%d %04x\n",
  488. word_len, *(rx - 1));
  489. #endif
  490. }
  491. } while (c);
  492. } else if (word_len <= 32) {
  493. u32 *rx;
  494. const u32 *tx;
  495. rx = xfer->rx_buf;
  496. tx = xfer->tx_buf;
  497. do {
  498. c -= 4;
  499. if (tx != NULL) {
  500. if (mcspi_wait_for_reg_bit(chstat_reg,
  501. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  502. dev_err(&spi->dev, "TXS timed out\n");
  503. goto out;
  504. }
  505. #ifdef VERBOSE
  506. dev_dbg(&spi->dev, "write-%d %08x\n",
  507. word_len, *tx);
  508. #endif
  509. __raw_writel(*tx++, tx_reg);
  510. }
  511. if (rx != NULL) {
  512. if (mcspi_wait_for_reg_bit(chstat_reg,
  513. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  514. dev_err(&spi->dev, "RXS timed out\n");
  515. goto out;
  516. }
  517. if (c == 4 && tx == NULL &&
  518. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  519. omap2_mcspi_set_enable(spi, 0);
  520. *rx++ = __raw_readl(rx_reg);
  521. #ifdef VERBOSE
  522. dev_dbg(&spi->dev, "read-%d %08x\n",
  523. word_len, *(rx - 1));
  524. #endif
  525. if (mcspi_wait_for_reg_bit(chstat_reg,
  526. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  527. dev_err(&spi->dev,
  528. "RXS timed out\n");
  529. goto out;
  530. }
  531. c = 0;
  532. } else if (c == 0 && tx == NULL) {
  533. omap2_mcspi_set_enable(spi, 0);
  534. }
  535. *rx++ = __raw_readl(rx_reg);
  536. #ifdef VERBOSE
  537. dev_dbg(&spi->dev, "read-%d %08x\n",
  538. word_len, *(rx - 1));
  539. #endif
  540. }
  541. } while (c);
  542. }
  543. /* for TX_ONLY mode, be sure all words have shifted out */
  544. if (xfer->rx_buf == NULL) {
  545. if (mcspi_wait_for_reg_bit(chstat_reg,
  546. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  547. dev_err(&spi->dev, "TXS timed out\n");
  548. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  549. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  550. dev_err(&spi->dev, "EOT timed out\n");
  551. }
  552. out:
  553. omap2_mcspi_set_enable(spi, 1);
  554. return count - c;
  555. }
  556. /* called only when no transfer is active to this device */
  557. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  558. struct spi_transfer *t)
  559. {
  560. struct omap2_mcspi_cs *cs = spi->controller_state;
  561. struct omap2_mcspi *mcspi;
  562. struct spi_master *spi_cntrl;
  563. u32 l = 0, div = 0;
  564. u8 word_len = spi->bits_per_word;
  565. u32 speed_hz = spi->max_speed_hz;
  566. mcspi = spi_master_get_devdata(spi->master);
  567. spi_cntrl = mcspi->master;
  568. if (t != NULL && t->bits_per_word)
  569. word_len = t->bits_per_word;
  570. cs->word_len = word_len;
  571. if (t && t->speed_hz)
  572. speed_hz = t->speed_hz;
  573. if (speed_hz) {
  574. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  575. > speed_hz)
  576. div++;
  577. } else
  578. div = 15;
  579. l = mcspi_cached_chconf0(spi);
  580. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  581. * REVISIT: this controller could support SPI_3WIRE mode.
  582. */
  583. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  584. l |= OMAP2_MCSPI_CHCONF_DPE0;
  585. /* wordlength */
  586. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  587. l |= (word_len - 1) << 7;
  588. /* set chipselect polarity; manage with FORCE */
  589. if (!(spi->mode & SPI_CS_HIGH))
  590. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  591. else
  592. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  593. /* set clock divisor */
  594. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  595. l |= div << 2;
  596. /* set SPI mode 0..3 */
  597. if (spi->mode & SPI_CPOL)
  598. l |= OMAP2_MCSPI_CHCONF_POL;
  599. else
  600. l &= ~OMAP2_MCSPI_CHCONF_POL;
  601. if (spi->mode & SPI_CPHA)
  602. l |= OMAP2_MCSPI_CHCONF_PHA;
  603. else
  604. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  605. mcspi_write_chconf0(spi, l);
  606. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  607. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  608. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  609. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  610. return 0;
  611. }
  612. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  613. {
  614. struct spi_device *spi = data;
  615. struct omap2_mcspi *mcspi;
  616. struct omap2_mcspi_dma *mcspi_dma;
  617. mcspi = spi_master_get_devdata(spi->master);
  618. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  619. complete(&mcspi_dma->dma_rx_completion);
  620. /* We must disable the DMA RX request */
  621. omap2_mcspi_set_dma_req(spi, 1, 0);
  622. }
  623. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  624. {
  625. struct spi_device *spi = data;
  626. struct omap2_mcspi *mcspi;
  627. struct omap2_mcspi_dma *mcspi_dma;
  628. mcspi = spi_master_get_devdata(spi->master);
  629. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  630. complete(&mcspi_dma->dma_tx_completion);
  631. /* We must disable the DMA TX request */
  632. omap2_mcspi_set_dma_req(spi, 0, 0);
  633. }
  634. static int omap2_mcspi_request_dma(struct spi_device *spi)
  635. {
  636. struct spi_master *master = spi->master;
  637. struct omap2_mcspi *mcspi;
  638. struct omap2_mcspi_dma *mcspi_dma;
  639. mcspi = spi_master_get_devdata(master);
  640. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  641. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  642. omap2_mcspi_dma_rx_callback, spi,
  643. &mcspi_dma->dma_rx_channel)) {
  644. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  645. return -EAGAIN;
  646. }
  647. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  648. omap2_mcspi_dma_tx_callback, spi,
  649. &mcspi_dma->dma_tx_channel)) {
  650. omap_free_dma(mcspi_dma->dma_rx_channel);
  651. mcspi_dma->dma_rx_channel = -1;
  652. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  653. return -EAGAIN;
  654. }
  655. init_completion(&mcspi_dma->dma_rx_completion);
  656. init_completion(&mcspi_dma->dma_tx_completion);
  657. return 0;
  658. }
  659. static int omap2_mcspi_setup(struct spi_device *spi)
  660. {
  661. int ret;
  662. struct omap2_mcspi *mcspi;
  663. struct omap2_mcspi_dma *mcspi_dma;
  664. struct omap2_mcspi_cs *cs = spi->controller_state;
  665. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  666. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  667. spi->bits_per_word);
  668. return -EINVAL;
  669. }
  670. mcspi = spi_master_get_devdata(spi->master);
  671. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  672. if (!cs) {
  673. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  674. if (!cs)
  675. return -ENOMEM;
  676. cs->base = mcspi->base + spi->chip_select * 0x14;
  677. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  678. cs->chconf0 = 0;
  679. spi->controller_state = cs;
  680. /* Link this to context save list */
  681. list_add_tail(&cs->node,
  682. &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
  683. }
  684. if (mcspi_dma->dma_rx_channel == -1
  685. || mcspi_dma->dma_tx_channel == -1) {
  686. ret = omap2_mcspi_request_dma(spi);
  687. if (ret < 0)
  688. return ret;
  689. }
  690. if (omap2_mcspi_enable_clocks(mcspi))
  691. return -ENODEV;
  692. ret = omap2_mcspi_setup_transfer(spi, NULL);
  693. omap2_mcspi_disable_clocks(mcspi);
  694. return ret;
  695. }
  696. static void omap2_mcspi_cleanup(struct spi_device *spi)
  697. {
  698. struct omap2_mcspi *mcspi;
  699. struct omap2_mcspi_dma *mcspi_dma;
  700. struct omap2_mcspi_cs *cs;
  701. mcspi = spi_master_get_devdata(spi->master);
  702. if (spi->controller_state) {
  703. /* Unlink controller state from context save list */
  704. cs = spi->controller_state;
  705. list_del(&cs->node);
  706. kfree(spi->controller_state);
  707. }
  708. if (spi->chip_select < spi->master->num_chipselect) {
  709. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  710. if (mcspi_dma->dma_rx_channel != -1) {
  711. omap_free_dma(mcspi_dma->dma_rx_channel);
  712. mcspi_dma->dma_rx_channel = -1;
  713. }
  714. if (mcspi_dma->dma_tx_channel != -1) {
  715. omap_free_dma(mcspi_dma->dma_tx_channel);
  716. mcspi_dma->dma_tx_channel = -1;
  717. }
  718. }
  719. }
  720. static void omap2_mcspi_work(struct work_struct *work)
  721. {
  722. struct omap2_mcspi *mcspi;
  723. mcspi = container_of(work, struct omap2_mcspi, work);
  724. spin_lock_irq(&mcspi->lock);
  725. if (omap2_mcspi_enable_clocks(mcspi))
  726. goto out;
  727. /* We only enable one channel at a time -- the one whose message is
  728. * at the head of the queue -- although this controller would gladly
  729. * arbitrate among multiple channels. This corresponds to "single
  730. * channel" master mode. As a side effect, we need to manage the
  731. * chipselect with the FORCE bit ... CS != channel enable.
  732. */
  733. while (!list_empty(&mcspi->msg_queue)) {
  734. struct spi_message *m;
  735. struct spi_device *spi;
  736. struct spi_transfer *t = NULL;
  737. int cs_active = 0;
  738. struct omap2_mcspi_cs *cs;
  739. struct omap2_mcspi_device_config *cd;
  740. int par_override = 0;
  741. int status = 0;
  742. u32 chconf;
  743. m = container_of(mcspi->msg_queue.next, struct spi_message,
  744. queue);
  745. list_del_init(&m->queue);
  746. spin_unlock_irq(&mcspi->lock);
  747. spi = m->spi;
  748. cs = spi->controller_state;
  749. cd = spi->controller_data;
  750. omap2_mcspi_set_enable(spi, 1);
  751. list_for_each_entry(t, &m->transfers, transfer_list) {
  752. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  753. status = -EINVAL;
  754. break;
  755. }
  756. if (par_override || t->speed_hz || t->bits_per_word) {
  757. par_override = 1;
  758. status = omap2_mcspi_setup_transfer(spi, t);
  759. if (status < 0)
  760. break;
  761. if (!t->speed_hz && !t->bits_per_word)
  762. par_override = 0;
  763. }
  764. if (!cs_active) {
  765. omap2_mcspi_force_cs(spi, 1);
  766. cs_active = 1;
  767. }
  768. chconf = mcspi_cached_chconf0(spi);
  769. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  770. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  771. if (t->tx_buf == NULL)
  772. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  773. else if (t->rx_buf == NULL)
  774. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  775. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  776. /* Turbo mode is for more than one word */
  777. if (t->len > ((cs->word_len + 7) >> 3))
  778. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  779. }
  780. mcspi_write_chconf0(spi, chconf);
  781. if (t->len) {
  782. unsigned count;
  783. /* RX_ONLY mode needs dummy data in TX reg */
  784. if (t->tx_buf == NULL)
  785. __raw_writel(0, cs->base
  786. + OMAP2_MCSPI_TX0);
  787. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  788. count = omap2_mcspi_txrx_dma(spi, t);
  789. else
  790. count = omap2_mcspi_txrx_pio(spi, t);
  791. m->actual_length += count;
  792. if (count != t->len) {
  793. status = -EIO;
  794. break;
  795. }
  796. }
  797. if (t->delay_usecs)
  798. udelay(t->delay_usecs);
  799. /* ignore the "leave it on after last xfer" hint */
  800. if (t->cs_change) {
  801. omap2_mcspi_force_cs(spi, 0);
  802. cs_active = 0;
  803. }
  804. }
  805. /* Restore defaults if they were overriden */
  806. if (par_override) {
  807. par_override = 0;
  808. status = omap2_mcspi_setup_transfer(spi, NULL);
  809. }
  810. if (cs_active)
  811. omap2_mcspi_force_cs(spi, 0);
  812. omap2_mcspi_set_enable(spi, 0);
  813. m->status = status;
  814. m->complete(m->context);
  815. spin_lock_irq(&mcspi->lock);
  816. }
  817. omap2_mcspi_disable_clocks(mcspi);
  818. out:
  819. spin_unlock_irq(&mcspi->lock);
  820. }
  821. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  822. {
  823. struct omap2_mcspi *mcspi;
  824. unsigned long flags;
  825. struct spi_transfer *t;
  826. m->actual_length = 0;
  827. m->status = 0;
  828. /* reject invalid messages and transfers */
  829. if (list_empty(&m->transfers) || !m->complete)
  830. return -EINVAL;
  831. list_for_each_entry(t, &m->transfers, transfer_list) {
  832. const void *tx_buf = t->tx_buf;
  833. void *rx_buf = t->rx_buf;
  834. unsigned len = t->len;
  835. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  836. || (len && !(rx_buf || tx_buf))
  837. || (t->bits_per_word &&
  838. ( t->bits_per_word < 4
  839. || t->bits_per_word > 32))) {
  840. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  841. t->speed_hz,
  842. len,
  843. tx_buf ? "tx" : "",
  844. rx_buf ? "rx" : "",
  845. t->bits_per_word);
  846. return -EINVAL;
  847. }
  848. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  849. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  850. t->speed_hz,
  851. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  852. return -EINVAL;
  853. }
  854. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  855. continue;
  856. /* Do DMA mapping "early" for better error reporting and
  857. * dcache use. Note that if dma_unmap_single() ever starts
  858. * to do real work on ARM, we'd need to clean up mappings
  859. * for previous transfers on *ALL* exits of this loop...
  860. */
  861. if (tx_buf != NULL) {
  862. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  863. len, DMA_TO_DEVICE);
  864. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  865. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  866. 'T', len);
  867. return -EINVAL;
  868. }
  869. }
  870. if (rx_buf != NULL) {
  871. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  872. DMA_FROM_DEVICE);
  873. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  874. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  875. 'R', len);
  876. if (tx_buf != NULL)
  877. dma_unmap_single(NULL, t->tx_dma,
  878. len, DMA_TO_DEVICE);
  879. return -EINVAL;
  880. }
  881. }
  882. }
  883. mcspi = spi_master_get_devdata(spi->master);
  884. spin_lock_irqsave(&mcspi->lock, flags);
  885. list_add_tail(&m->queue, &mcspi->msg_queue);
  886. queue_work(omap2_mcspi_wq, &mcspi->work);
  887. spin_unlock_irqrestore(&mcspi->lock, flags);
  888. return 0;
  889. }
  890. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  891. {
  892. struct spi_master *master = mcspi->master;
  893. u32 tmp;
  894. if (omap2_mcspi_enable_clocks(mcspi))
  895. return -1;
  896. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  897. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  898. do {
  899. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  900. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  901. tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
  902. OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
  903. OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
  904. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
  905. omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
  906. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  907. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  908. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  909. omap2_mcspi_set_master_mode(master);
  910. omap2_mcspi_disable_clocks(mcspi);
  911. return 0;
  912. }
  913. static u8 __initdata spi1_rxdma_id [] = {
  914. OMAP24XX_DMA_SPI1_RX0,
  915. OMAP24XX_DMA_SPI1_RX1,
  916. OMAP24XX_DMA_SPI1_RX2,
  917. OMAP24XX_DMA_SPI1_RX3,
  918. };
  919. static u8 __initdata spi1_txdma_id [] = {
  920. OMAP24XX_DMA_SPI1_TX0,
  921. OMAP24XX_DMA_SPI1_TX1,
  922. OMAP24XX_DMA_SPI1_TX2,
  923. OMAP24XX_DMA_SPI1_TX3,
  924. };
  925. static u8 __initdata spi2_rxdma_id[] = {
  926. OMAP24XX_DMA_SPI2_RX0,
  927. OMAP24XX_DMA_SPI2_RX1,
  928. };
  929. static u8 __initdata spi2_txdma_id[] = {
  930. OMAP24XX_DMA_SPI2_TX0,
  931. OMAP24XX_DMA_SPI2_TX1,
  932. };
  933. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  934. || defined(CONFIG_ARCH_OMAP4)
  935. static u8 __initdata spi3_rxdma_id[] = {
  936. OMAP24XX_DMA_SPI3_RX0,
  937. OMAP24XX_DMA_SPI3_RX1,
  938. };
  939. static u8 __initdata spi3_txdma_id[] = {
  940. OMAP24XX_DMA_SPI3_TX0,
  941. OMAP24XX_DMA_SPI3_TX1,
  942. };
  943. #endif
  944. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  945. static u8 __initdata spi4_rxdma_id[] = {
  946. OMAP34XX_DMA_SPI4_RX0,
  947. };
  948. static u8 __initdata spi4_txdma_id[] = {
  949. OMAP34XX_DMA_SPI4_TX0,
  950. };
  951. #endif
  952. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  953. {
  954. struct spi_master *master;
  955. struct omap2_mcspi *mcspi;
  956. struct resource *r;
  957. int status = 0, i;
  958. const u8 *rxdma_id, *txdma_id;
  959. unsigned num_chipselect;
  960. switch (pdev->id) {
  961. case 1:
  962. rxdma_id = spi1_rxdma_id;
  963. txdma_id = spi1_txdma_id;
  964. num_chipselect = 4;
  965. break;
  966. case 2:
  967. rxdma_id = spi2_rxdma_id;
  968. txdma_id = spi2_txdma_id;
  969. num_chipselect = 2;
  970. break;
  971. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  972. || defined(CONFIG_ARCH_OMAP4)
  973. case 3:
  974. rxdma_id = spi3_rxdma_id;
  975. txdma_id = spi3_txdma_id;
  976. num_chipselect = 2;
  977. break;
  978. #endif
  979. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  980. case 4:
  981. rxdma_id = spi4_rxdma_id;
  982. txdma_id = spi4_txdma_id;
  983. num_chipselect = 1;
  984. break;
  985. #endif
  986. default:
  987. return -EINVAL;
  988. }
  989. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  990. if (master == NULL) {
  991. dev_dbg(&pdev->dev, "master allocation failed\n");
  992. return -ENOMEM;
  993. }
  994. /* the spi->mode bits understood by this driver: */
  995. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  996. if (pdev->id != -1)
  997. master->bus_num = pdev->id;
  998. master->setup = omap2_mcspi_setup;
  999. master->transfer = omap2_mcspi_transfer;
  1000. master->cleanup = omap2_mcspi_cleanup;
  1001. master->num_chipselect = num_chipselect;
  1002. dev_set_drvdata(&pdev->dev, master);
  1003. mcspi = spi_master_get_devdata(master);
  1004. mcspi->master = master;
  1005. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1006. if (r == NULL) {
  1007. status = -ENODEV;
  1008. goto err1;
  1009. }
  1010. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  1011. dev_name(&pdev->dev))) {
  1012. status = -EBUSY;
  1013. goto err1;
  1014. }
  1015. mcspi->phys = r->start;
  1016. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  1017. if (!mcspi->base) {
  1018. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  1019. status = -ENOMEM;
  1020. goto err1aa;
  1021. }
  1022. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  1023. spin_lock_init(&mcspi->lock);
  1024. INIT_LIST_HEAD(&mcspi->msg_queue);
  1025. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  1026. mcspi->ick = clk_get(&pdev->dev, "ick");
  1027. if (IS_ERR(mcspi->ick)) {
  1028. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  1029. status = PTR_ERR(mcspi->ick);
  1030. goto err1a;
  1031. }
  1032. mcspi->fck = clk_get(&pdev->dev, "fck");
  1033. if (IS_ERR(mcspi->fck)) {
  1034. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  1035. status = PTR_ERR(mcspi->fck);
  1036. goto err2;
  1037. }
  1038. mcspi->dma_channels = kcalloc(master->num_chipselect,
  1039. sizeof(struct omap2_mcspi_dma),
  1040. GFP_KERNEL);
  1041. if (mcspi->dma_channels == NULL)
  1042. goto err3;
  1043. for (i = 0; i < num_chipselect; i++) {
  1044. mcspi->dma_channels[i].dma_rx_channel = -1;
  1045. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  1046. mcspi->dma_channels[i].dma_tx_channel = -1;
  1047. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  1048. }
  1049. if (omap2_mcspi_reset(mcspi) < 0)
  1050. goto err4;
  1051. status = spi_register_master(master);
  1052. if (status < 0)
  1053. goto err4;
  1054. return status;
  1055. err4:
  1056. kfree(mcspi->dma_channels);
  1057. err3:
  1058. clk_put(mcspi->fck);
  1059. err2:
  1060. clk_put(mcspi->ick);
  1061. err1a:
  1062. iounmap(mcspi->base);
  1063. err1aa:
  1064. release_mem_region(r->start, (r->end - r->start) + 1);
  1065. err1:
  1066. spi_master_put(master);
  1067. return status;
  1068. }
  1069. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  1070. {
  1071. struct spi_master *master;
  1072. struct omap2_mcspi *mcspi;
  1073. struct omap2_mcspi_dma *dma_channels;
  1074. struct resource *r;
  1075. void __iomem *base;
  1076. master = dev_get_drvdata(&pdev->dev);
  1077. mcspi = spi_master_get_devdata(master);
  1078. dma_channels = mcspi->dma_channels;
  1079. clk_put(mcspi->fck);
  1080. clk_put(mcspi->ick);
  1081. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1082. release_mem_region(r->start, (r->end - r->start) + 1);
  1083. base = mcspi->base;
  1084. spi_unregister_master(master);
  1085. iounmap(base);
  1086. kfree(dma_channels);
  1087. return 0;
  1088. }
  1089. /* work with hotplug and coldplug */
  1090. MODULE_ALIAS("platform:omap2_mcspi");
  1091. static struct platform_driver omap2_mcspi_driver = {
  1092. .driver = {
  1093. .name = "omap2_mcspi",
  1094. .owner = THIS_MODULE,
  1095. },
  1096. .remove = __exit_p(omap2_mcspi_remove),
  1097. };
  1098. static int __init omap2_mcspi_init(void)
  1099. {
  1100. omap2_mcspi_wq = create_singlethread_workqueue(
  1101. omap2_mcspi_driver.driver.name);
  1102. if (omap2_mcspi_wq == NULL)
  1103. return -1;
  1104. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1105. }
  1106. subsys_initcall(omap2_mcspi_init);
  1107. static void __exit omap2_mcspi_exit(void)
  1108. {
  1109. platform_driver_unregister(&omap2_mcspi_driver);
  1110. destroy_workqueue(omap2_mcspi_wq);
  1111. }
  1112. module_exit(omap2_mcspi_exit);
  1113. MODULE_LICENSE("GPL");