spi-pl022.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  1. /*
  2. * A driver for the ARM PL022 PrimeCell SSP/SPI bus master.
  3. *
  4. * Copyright (C) 2008-2009 ST-Ericsson AB
  5. * Copyright (C) 2006 STMicroelectronics Pvt. Ltd.
  6. *
  7. * Author: Linus Walleij <linus.walleij@stericsson.com>
  8. *
  9. * Initial version inspired by:
  10. * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
  11. * Initial adoption to PL022 by:
  12. * Sachin Verma <sachin.verma@st.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/ioport.h>
  28. #include <linux/errno.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/delay.h>
  32. #include <linux/clk.h>
  33. #include <linux/err.h>
  34. #include <linux/amba/bus.h>
  35. #include <linux/amba/pl022.h>
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/scatterlist.h>
  41. #include <linux/pm_runtime.h>
  42. /*
  43. * This macro is used to define some register default values.
  44. * reg is masked with mask, the OR:ed with an (again masked)
  45. * val shifted sb steps to the left.
  46. */
  47. #define SSP_WRITE_BITS(reg, val, mask, sb) \
  48. ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
  49. /*
  50. * This macro is also used to define some default values.
  51. * It will just shift val by sb steps to the left and mask
  52. * the result with mask.
  53. */
  54. #define GEN_MASK_BITS(val, mask, sb) \
  55. (((val)<<(sb)) & (mask))
  56. #define DRIVE_TX 0
  57. #define DO_NOT_DRIVE_TX 1
  58. #define DO_NOT_QUEUE_DMA 0
  59. #define QUEUE_DMA 1
  60. #define RX_TRANSFER 1
  61. #define TX_TRANSFER 2
  62. /*
  63. * Macros to access SSP Registers with their offsets
  64. */
  65. #define SSP_CR0(r) (r + 0x000)
  66. #define SSP_CR1(r) (r + 0x004)
  67. #define SSP_DR(r) (r + 0x008)
  68. #define SSP_SR(r) (r + 0x00C)
  69. #define SSP_CPSR(r) (r + 0x010)
  70. #define SSP_IMSC(r) (r + 0x014)
  71. #define SSP_RIS(r) (r + 0x018)
  72. #define SSP_MIS(r) (r + 0x01C)
  73. #define SSP_ICR(r) (r + 0x020)
  74. #define SSP_DMACR(r) (r + 0x024)
  75. #define SSP_ITCR(r) (r + 0x080)
  76. #define SSP_ITIP(r) (r + 0x084)
  77. #define SSP_ITOP(r) (r + 0x088)
  78. #define SSP_TDR(r) (r + 0x08C)
  79. #define SSP_PID0(r) (r + 0xFE0)
  80. #define SSP_PID1(r) (r + 0xFE4)
  81. #define SSP_PID2(r) (r + 0xFE8)
  82. #define SSP_PID3(r) (r + 0xFEC)
  83. #define SSP_CID0(r) (r + 0xFF0)
  84. #define SSP_CID1(r) (r + 0xFF4)
  85. #define SSP_CID2(r) (r + 0xFF8)
  86. #define SSP_CID3(r) (r + 0xFFC)
  87. /*
  88. * SSP Control Register 0 - SSP_CR0
  89. */
  90. #define SSP_CR0_MASK_DSS (0x0FUL << 0)
  91. #define SSP_CR0_MASK_FRF (0x3UL << 4)
  92. #define SSP_CR0_MASK_SPO (0x1UL << 6)
  93. #define SSP_CR0_MASK_SPH (0x1UL << 7)
  94. #define SSP_CR0_MASK_SCR (0xFFUL << 8)
  95. /*
  96. * The ST version of this block moves som bits
  97. * in SSP_CR0 and extends it to 32 bits
  98. */
  99. #define SSP_CR0_MASK_DSS_ST (0x1FUL << 0)
  100. #define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5)
  101. #define SSP_CR0_MASK_CSS_ST (0x1FUL << 16)
  102. #define SSP_CR0_MASK_FRF_ST (0x3UL << 21)
  103. /*
  104. * SSP Control Register 0 - SSP_CR1
  105. */
  106. #define SSP_CR1_MASK_LBM (0x1UL << 0)
  107. #define SSP_CR1_MASK_SSE (0x1UL << 1)
  108. #define SSP_CR1_MASK_MS (0x1UL << 2)
  109. #define SSP_CR1_MASK_SOD (0x1UL << 3)
  110. /*
  111. * The ST version of this block adds some bits
  112. * in SSP_CR1
  113. */
  114. #define SSP_CR1_MASK_RENDN_ST (0x1UL << 4)
  115. #define SSP_CR1_MASK_TENDN_ST (0x1UL << 5)
  116. #define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6)
  117. #define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
  118. #define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
  119. /* This one is only in the PL023 variant */
  120. #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
  121. /*
  122. * SSP Status Register - SSP_SR
  123. */
  124. #define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
  125. #define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
  126. #define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
  127. #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
  128. #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
  129. /*
  130. * SSP Clock Prescale Register - SSP_CPSR
  131. */
  132. #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
  133. /*
  134. * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
  135. */
  136. #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
  137. #define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
  138. #define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
  139. #define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
  140. /*
  141. * SSP Raw Interrupt Status Register - SSP_RIS
  142. */
  143. /* Receive Overrun Raw Interrupt status */
  144. #define SSP_RIS_MASK_RORRIS (0x1UL << 0)
  145. /* Receive Timeout Raw Interrupt status */
  146. #define SSP_RIS_MASK_RTRIS (0x1UL << 1)
  147. /* Receive FIFO Raw Interrupt status */
  148. #define SSP_RIS_MASK_RXRIS (0x1UL << 2)
  149. /* Transmit FIFO Raw Interrupt status */
  150. #define SSP_RIS_MASK_TXRIS (0x1UL << 3)
  151. /*
  152. * SSP Masked Interrupt Status Register - SSP_MIS
  153. */
  154. /* Receive Overrun Masked Interrupt status */
  155. #define SSP_MIS_MASK_RORMIS (0x1UL << 0)
  156. /* Receive Timeout Masked Interrupt status */
  157. #define SSP_MIS_MASK_RTMIS (0x1UL << 1)
  158. /* Receive FIFO Masked Interrupt status */
  159. #define SSP_MIS_MASK_RXMIS (0x1UL << 2)
  160. /* Transmit FIFO Masked Interrupt status */
  161. #define SSP_MIS_MASK_TXMIS (0x1UL << 3)
  162. /*
  163. * SSP Interrupt Clear Register - SSP_ICR
  164. */
  165. /* Receive Overrun Raw Clear Interrupt bit */
  166. #define SSP_ICR_MASK_RORIC (0x1UL << 0)
  167. /* Receive Timeout Clear Interrupt bit */
  168. #define SSP_ICR_MASK_RTIC (0x1UL << 1)
  169. /*
  170. * SSP DMA Control Register - SSP_DMACR
  171. */
  172. /* Receive DMA Enable bit */
  173. #define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
  174. /* Transmit DMA Enable bit */
  175. #define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
  176. /*
  177. * SSP Integration Test control Register - SSP_ITCR
  178. */
  179. #define SSP_ITCR_MASK_ITEN (0x1UL << 0)
  180. #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
  181. /*
  182. * SSP Integration Test Input Register - SSP_ITIP
  183. */
  184. #define ITIP_MASK_SSPRXD (0x1UL << 0)
  185. #define ITIP_MASK_SSPFSSIN (0x1UL << 1)
  186. #define ITIP_MASK_SSPCLKIN (0x1UL << 2)
  187. #define ITIP_MASK_RXDMAC (0x1UL << 3)
  188. #define ITIP_MASK_TXDMAC (0x1UL << 4)
  189. #define ITIP_MASK_SSPTXDIN (0x1UL << 5)
  190. /*
  191. * SSP Integration Test output Register - SSP_ITOP
  192. */
  193. #define ITOP_MASK_SSPTXD (0x1UL << 0)
  194. #define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
  195. #define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
  196. #define ITOP_MASK_SSPOEn (0x1UL << 3)
  197. #define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
  198. #define ITOP_MASK_RORINTR (0x1UL << 5)
  199. #define ITOP_MASK_RTINTR (0x1UL << 6)
  200. #define ITOP_MASK_RXINTR (0x1UL << 7)
  201. #define ITOP_MASK_TXINTR (0x1UL << 8)
  202. #define ITOP_MASK_INTR (0x1UL << 9)
  203. #define ITOP_MASK_RXDMABREQ (0x1UL << 10)
  204. #define ITOP_MASK_RXDMASREQ (0x1UL << 11)
  205. #define ITOP_MASK_TXDMABREQ (0x1UL << 12)
  206. #define ITOP_MASK_TXDMASREQ (0x1UL << 13)
  207. /*
  208. * SSP Test Data Register - SSP_TDR
  209. */
  210. #define TDR_MASK_TESTDATA (0xFFFFFFFF)
  211. /*
  212. * Message State
  213. * we use the spi_message.state (void *) pointer to
  214. * hold a single state value, that's why all this
  215. * (void *) casting is done here.
  216. */
  217. #define STATE_START ((void *) 0)
  218. #define STATE_RUNNING ((void *) 1)
  219. #define STATE_DONE ((void *) 2)
  220. #define STATE_ERROR ((void *) -1)
  221. /*
  222. * SSP State - Whether Enabled or Disabled
  223. */
  224. #define SSP_DISABLED (0)
  225. #define SSP_ENABLED (1)
  226. /*
  227. * SSP DMA State - Whether DMA Enabled or Disabled
  228. */
  229. #define SSP_DMA_DISABLED (0)
  230. #define SSP_DMA_ENABLED (1)
  231. /*
  232. * SSP Clock Defaults
  233. */
  234. #define SSP_DEFAULT_CLKRATE 0x2
  235. #define SSP_DEFAULT_PRESCALE 0x40
  236. /*
  237. * SSP Clock Parameter ranges
  238. */
  239. #define CPSDVR_MIN 0x02
  240. #define CPSDVR_MAX 0xFE
  241. #define SCR_MIN 0x00
  242. #define SCR_MAX 0xFF
  243. /*
  244. * SSP Interrupt related Macros
  245. */
  246. #define DEFAULT_SSP_REG_IMSC 0x0UL
  247. #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
  248. #define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC)
  249. #define CLEAR_ALL_INTERRUPTS 0x3
  250. #define SPI_POLLING_TIMEOUT 1000
  251. /*
  252. * The type of reading going on on this chip
  253. */
  254. enum ssp_reading {
  255. READING_NULL,
  256. READING_U8,
  257. READING_U16,
  258. READING_U32
  259. };
  260. /**
  261. * The type of writing going on on this chip
  262. */
  263. enum ssp_writing {
  264. WRITING_NULL,
  265. WRITING_U8,
  266. WRITING_U16,
  267. WRITING_U32
  268. };
  269. /**
  270. * struct vendor_data - vendor-specific config parameters
  271. * for PL022 derivates
  272. * @fifodepth: depth of FIFOs (both)
  273. * @max_bpw: maximum number of bits per word
  274. * @unidir: supports unidirection transfers
  275. * @extended_cr: 32 bit wide control register 0 with extra
  276. * features and extra features in CR1 as found in the ST variants
  277. * @pl023: supports a subset of the ST extensions called "PL023"
  278. */
  279. struct vendor_data {
  280. int fifodepth;
  281. int max_bpw;
  282. bool unidir;
  283. bool extended_cr;
  284. bool pl023;
  285. bool loopback;
  286. };
  287. /**
  288. * struct pl022 - This is the private SSP driver data structure
  289. * @adev: AMBA device model hookup
  290. * @vendor: vendor data for the IP block
  291. * @phybase: the physical memory where the SSP device resides
  292. * @virtbase: the virtual memory where the SSP is mapped
  293. * @clk: outgoing clock "SPICLK" for the SPI bus
  294. * @master: SPI framework hookup
  295. * @master_info: controller-specific data from machine setup
  296. * @kworker: thread struct for message pump
  297. * @kworker_task: pointer to task for message pump kworker thread
  298. * @pump_messages: work struct for scheduling work to the message pump
  299. * @queue_lock: spinlock to syncronise access to message queue
  300. * @queue: message queue
  301. * @busy: message pump is busy
  302. * @running: message pump is running
  303. * @pump_transfers: Tasklet used in Interrupt Transfer mode
  304. * @cur_msg: Pointer to current spi_message being processed
  305. * @cur_transfer: Pointer to current spi_transfer
  306. * @cur_chip: pointer to current clients chip(assigned from controller_state)
  307. * @next_msg_cs_active: the next message in the queue has been examined
  308. * and it was found that it uses the same chip select as the previous
  309. * message, so we left it active after the previous transfer, and it's
  310. * active already.
  311. * @tx: current position in TX buffer to be read
  312. * @tx_end: end position in TX buffer to be read
  313. * @rx: current position in RX buffer to be written
  314. * @rx_end: end position in RX buffer to be written
  315. * @read: the type of read currently going on
  316. * @write: the type of write currently going on
  317. * @exp_fifo_level: expected FIFO level
  318. * @dma_rx_channel: optional channel for RX DMA
  319. * @dma_tx_channel: optional channel for TX DMA
  320. * @sgt_rx: scattertable for the RX transfer
  321. * @sgt_tx: scattertable for the TX transfer
  322. * @dummypage: a dummy page used for driving data on the bus with DMA
  323. */
  324. struct pl022 {
  325. struct amba_device *adev;
  326. struct vendor_data *vendor;
  327. resource_size_t phybase;
  328. void __iomem *virtbase;
  329. struct clk *clk;
  330. struct spi_master *master;
  331. struct pl022_ssp_controller *master_info;
  332. /* Message per-transfer pump */
  333. struct tasklet_struct pump_transfers;
  334. struct spi_message *cur_msg;
  335. struct spi_transfer *cur_transfer;
  336. struct chip_data *cur_chip;
  337. bool next_msg_cs_active;
  338. void *tx;
  339. void *tx_end;
  340. void *rx;
  341. void *rx_end;
  342. enum ssp_reading read;
  343. enum ssp_writing write;
  344. u32 exp_fifo_level;
  345. enum ssp_rx_level_trig rx_lev_trig;
  346. enum ssp_tx_level_trig tx_lev_trig;
  347. /* DMA settings */
  348. #ifdef CONFIG_DMA_ENGINE
  349. struct dma_chan *dma_rx_channel;
  350. struct dma_chan *dma_tx_channel;
  351. struct sg_table sgt_rx;
  352. struct sg_table sgt_tx;
  353. char *dummypage;
  354. bool dma_running;
  355. #endif
  356. };
  357. /**
  358. * struct chip_data - To maintain runtime state of SSP for each client chip
  359. * @cr0: Value of control register CR0 of SSP - on later ST variants this
  360. * register is 32 bits wide rather than just 16
  361. * @cr1: Value of control register CR1 of SSP
  362. * @dmacr: Value of DMA control Register of SSP
  363. * @cpsr: Value of Clock prescale register
  364. * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
  365. * @enable_dma: Whether to enable DMA or not
  366. * @read: function ptr to be used to read when doing xfer for this chip
  367. * @write: function ptr to be used to write when doing xfer for this chip
  368. * @cs_control: chip select callback provided by chip
  369. * @xfer_type: polling/interrupt/DMA
  370. *
  371. * Runtime state of the SSP controller, maintained per chip,
  372. * This would be set according to the current message that would be served
  373. */
  374. struct chip_data {
  375. u32 cr0;
  376. u16 cr1;
  377. u16 dmacr;
  378. u16 cpsr;
  379. u8 n_bytes;
  380. bool enable_dma;
  381. enum ssp_reading read;
  382. enum ssp_writing write;
  383. void (*cs_control) (u32 command);
  384. int xfer_type;
  385. };
  386. /**
  387. * null_cs_control - Dummy chip select function
  388. * @command: select/delect the chip
  389. *
  390. * If no chip select function is provided by client this is used as dummy
  391. * chip select
  392. */
  393. static void null_cs_control(u32 command)
  394. {
  395. pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
  396. }
  397. /**
  398. * giveback - current spi_message is over, schedule next message and call
  399. * callback of this message. Assumes that caller already
  400. * set message->status; dma and pio irqs are blocked
  401. * @pl022: SSP driver private data structure
  402. */
  403. static void giveback(struct pl022 *pl022)
  404. {
  405. struct spi_transfer *last_transfer;
  406. pl022->next_msg_cs_active = false;
  407. last_transfer = list_entry(pl022->cur_msg->transfers.prev,
  408. struct spi_transfer,
  409. transfer_list);
  410. /* Delay if requested before any change in chip select */
  411. if (last_transfer->delay_usecs)
  412. /*
  413. * FIXME: This runs in interrupt context.
  414. * Is this really smart?
  415. */
  416. udelay(last_transfer->delay_usecs);
  417. if (!last_transfer->cs_change) {
  418. struct spi_message *next_msg;
  419. /*
  420. * cs_change was not set. We can keep the chip select
  421. * enabled if there is message in the queue and it is
  422. * for the same spi device.
  423. *
  424. * We cannot postpone this until pump_messages, because
  425. * after calling msg->complete (below) the driver that
  426. * sent the current message could be unloaded, which
  427. * could invalidate the cs_control() callback...
  428. */
  429. /* get a pointer to the next message, if any */
  430. next_msg = spi_get_next_queued_message(pl022->master);
  431. /*
  432. * see if the next and current messages point
  433. * to the same spi device.
  434. */
  435. if (next_msg && next_msg->spi != pl022->cur_msg->spi)
  436. next_msg = NULL;
  437. if (!next_msg || pl022->cur_msg->state == STATE_ERROR)
  438. pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
  439. else
  440. pl022->next_msg_cs_active = true;
  441. }
  442. pl022->cur_msg = NULL;
  443. pl022->cur_transfer = NULL;
  444. pl022->cur_chip = NULL;
  445. spi_finalize_current_message(pl022->master);
  446. }
  447. /**
  448. * flush - flush the FIFO to reach a clean state
  449. * @pl022: SSP driver private data structure
  450. */
  451. static int flush(struct pl022 *pl022)
  452. {
  453. unsigned long limit = loops_per_jiffy << 1;
  454. dev_dbg(&pl022->adev->dev, "flush\n");
  455. do {
  456. while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  457. readw(SSP_DR(pl022->virtbase));
  458. } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
  459. pl022->exp_fifo_level = 0;
  460. return limit;
  461. }
  462. /**
  463. * restore_state - Load configuration of current chip
  464. * @pl022: SSP driver private data structure
  465. */
  466. static void restore_state(struct pl022 *pl022)
  467. {
  468. struct chip_data *chip = pl022->cur_chip;
  469. if (pl022->vendor->extended_cr)
  470. writel(chip->cr0, SSP_CR0(pl022->virtbase));
  471. else
  472. writew(chip->cr0, SSP_CR0(pl022->virtbase));
  473. writew(chip->cr1, SSP_CR1(pl022->virtbase));
  474. writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
  475. writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
  476. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  477. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  478. }
  479. /*
  480. * Default SSP Register Values
  481. */
  482. #define DEFAULT_SSP_REG_CR0 ( \
  483. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
  484. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
  485. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  486. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  487. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  488. )
  489. /* ST versions have slightly different bit layout */
  490. #define DEFAULT_SSP_REG_CR0_ST ( \
  491. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  492. GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
  493. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  494. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  495. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
  496. GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
  497. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
  498. )
  499. /* The PL023 version is slightly different again */
  500. #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
  501. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  502. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  503. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  504. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  505. )
  506. #define DEFAULT_SSP_REG_CR1 ( \
  507. GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
  508. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  509. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  510. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
  511. )
  512. /* ST versions extend this register to use all 16 bits */
  513. #define DEFAULT_SSP_REG_CR1_ST ( \
  514. DEFAULT_SSP_REG_CR1 | \
  515. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  516. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  517. GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
  518. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  519. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
  520. )
  521. /*
  522. * The PL023 variant has further differences: no loopback mode, no microwire
  523. * support, and a new clock feedback delay setting.
  524. */
  525. #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
  526. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  527. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  528. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
  529. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  530. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  531. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  532. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
  533. GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
  534. )
  535. #define DEFAULT_SSP_REG_CPSR ( \
  536. GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
  537. )
  538. #define DEFAULT_SSP_REG_DMACR (\
  539. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
  540. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
  541. )
  542. /**
  543. * load_ssp_default_config - Load default configuration for SSP
  544. * @pl022: SSP driver private data structure
  545. */
  546. static void load_ssp_default_config(struct pl022 *pl022)
  547. {
  548. if (pl022->vendor->pl023) {
  549. writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
  550. writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
  551. } else if (pl022->vendor->extended_cr) {
  552. writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
  553. writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
  554. } else {
  555. writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
  556. writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
  557. }
  558. writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
  559. writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
  560. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  561. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  562. }
  563. /**
  564. * This will write to TX and read from RX according to the parameters
  565. * set in pl022.
  566. */
  567. static void readwriter(struct pl022 *pl022)
  568. {
  569. /*
  570. * The FIFO depth is different between primecell variants.
  571. * I believe filling in too much in the FIFO might cause
  572. * errons in 8bit wide transfers on ARM variants (just 8 words
  573. * FIFO, means only 8x8 = 64 bits in FIFO) at least.
  574. *
  575. * To prevent this issue, the TX FIFO is only filled to the
  576. * unused RX FIFO fill length, regardless of what the TX
  577. * FIFO status flag indicates.
  578. */
  579. dev_dbg(&pl022->adev->dev,
  580. "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
  581. __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
  582. /* Read as much as you can */
  583. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  584. && (pl022->rx < pl022->rx_end)) {
  585. switch (pl022->read) {
  586. case READING_NULL:
  587. readw(SSP_DR(pl022->virtbase));
  588. break;
  589. case READING_U8:
  590. *(u8 *) (pl022->rx) =
  591. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  592. break;
  593. case READING_U16:
  594. *(u16 *) (pl022->rx) =
  595. (u16) readw(SSP_DR(pl022->virtbase));
  596. break;
  597. case READING_U32:
  598. *(u32 *) (pl022->rx) =
  599. readl(SSP_DR(pl022->virtbase));
  600. break;
  601. }
  602. pl022->rx += (pl022->cur_chip->n_bytes);
  603. pl022->exp_fifo_level--;
  604. }
  605. /*
  606. * Write as much as possible up to the RX FIFO size
  607. */
  608. while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
  609. && (pl022->tx < pl022->tx_end)) {
  610. switch (pl022->write) {
  611. case WRITING_NULL:
  612. writew(0x0, SSP_DR(pl022->virtbase));
  613. break;
  614. case WRITING_U8:
  615. writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
  616. break;
  617. case WRITING_U16:
  618. writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
  619. break;
  620. case WRITING_U32:
  621. writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
  622. break;
  623. }
  624. pl022->tx += (pl022->cur_chip->n_bytes);
  625. pl022->exp_fifo_level++;
  626. /*
  627. * This inner reader takes care of things appearing in the RX
  628. * FIFO as we're transmitting. This will happen a lot since the
  629. * clock starts running when you put things into the TX FIFO,
  630. * and then things are continuously clocked into the RX FIFO.
  631. */
  632. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  633. && (pl022->rx < pl022->rx_end)) {
  634. switch (pl022->read) {
  635. case READING_NULL:
  636. readw(SSP_DR(pl022->virtbase));
  637. break;
  638. case READING_U8:
  639. *(u8 *) (pl022->rx) =
  640. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  641. break;
  642. case READING_U16:
  643. *(u16 *) (pl022->rx) =
  644. (u16) readw(SSP_DR(pl022->virtbase));
  645. break;
  646. case READING_U32:
  647. *(u32 *) (pl022->rx) =
  648. readl(SSP_DR(pl022->virtbase));
  649. break;
  650. }
  651. pl022->rx += (pl022->cur_chip->n_bytes);
  652. pl022->exp_fifo_level--;
  653. }
  654. }
  655. /*
  656. * When we exit here the TX FIFO should be full and the RX FIFO
  657. * should be empty
  658. */
  659. }
  660. /**
  661. * next_transfer - Move to the Next transfer in the current spi message
  662. * @pl022: SSP driver private data structure
  663. *
  664. * This function moves though the linked list of spi transfers in the
  665. * current spi message and returns with the state of current spi
  666. * message i.e whether its last transfer is done(STATE_DONE) or
  667. * Next transfer is ready(STATE_RUNNING)
  668. */
  669. static void *next_transfer(struct pl022 *pl022)
  670. {
  671. struct spi_message *msg = pl022->cur_msg;
  672. struct spi_transfer *trans = pl022->cur_transfer;
  673. /* Move to next transfer */
  674. if (trans->transfer_list.next != &msg->transfers) {
  675. pl022->cur_transfer =
  676. list_entry(trans->transfer_list.next,
  677. struct spi_transfer, transfer_list);
  678. return STATE_RUNNING;
  679. }
  680. return STATE_DONE;
  681. }
  682. /*
  683. * This DMA functionality is only compiled in if we have
  684. * access to the generic DMA devices/DMA engine.
  685. */
  686. #ifdef CONFIG_DMA_ENGINE
  687. static void unmap_free_dma_scatter(struct pl022 *pl022)
  688. {
  689. /* Unmap and free the SG tables */
  690. dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
  691. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  692. dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
  693. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  694. sg_free_table(&pl022->sgt_rx);
  695. sg_free_table(&pl022->sgt_tx);
  696. }
  697. static void dma_callback(void *data)
  698. {
  699. struct pl022 *pl022 = data;
  700. struct spi_message *msg = pl022->cur_msg;
  701. BUG_ON(!pl022->sgt_rx.sgl);
  702. #ifdef VERBOSE_DEBUG
  703. /*
  704. * Optionally dump out buffers to inspect contents, this is
  705. * good if you want to convince yourself that the loopback
  706. * read/write contents are the same, when adopting to a new
  707. * DMA engine.
  708. */
  709. {
  710. struct scatterlist *sg;
  711. unsigned int i;
  712. dma_sync_sg_for_cpu(&pl022->adev->dev,
  713. pl022->sgt_rx.sgl,
  714. pl022->sgt_rx.nents,
  715. DMA_FROM_DEVICE);
  716. for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
  717. dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
  718. print_hex_dump(KERN_ERR, "SPI RX: ",
  719. DUMP_PREFIX_OFFSET,
  720. 16,
  721. 1,
  722. sg_virt(sg),
  723. sg_dma_len(sg),
  724. 1);
  725. }
  726. for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
  727. dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
  728. print_hex_dump(KERN_ERR, "SPI TX: ",
  729. DUMP_PREFIX_OFFSET,
  730. 16,
  731. 1,
  732. sg_virt(sg),
  733. sg_dma_len(sg),
  734. 1);
  735. }
  736. }
  737. #endif
  738. unmap_free_dma_scatter(pl022);
  739. /* Update total bytes transferred */
  740. msg->actual_length += pl022->cur_transfer->len;
  741. if (pl022->cur_transfer->cs_change)
  742. pl022->cur_chip->
  743. cs_control(SSP_CHIP_DESELECT);
  744. /* Move to next transfer */
  745. msg->state = next_transfer(pl022);
  746. tasklet_schedule(&pl022->pump_transfers);
  747. }
  748. static void setup_dma_scatter(struct pl022 *pl022,
  749. void *buffer,
  750. unsigned int length,
  751. struct sg_table *sgtab)
  752. {
  753. struct scatterlist *sg;
  754. int bytesleft = length;
  755. void *bufp = buffer;
  756. int mapbytes;
  757. int i;
  758. if (buffer) {
  759. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  760. /*
  761. * If there are less bytes left than what fits
  762. * in the current page (plus page alignment offset)
  763. * we just feed in this, else we stuff in as much
  764. * as we can.
  765. */
  766. if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
  767. mapbytes = bytesleft;
  768. else
  769. mapbytes = PAGE_SIZE - offset_in_page(bufp);
  770. sg_set_page(sg, virt_to_page(bufp),
  771. mapbytes, offset_in_page(bufp));
  772. bufp += mapbytes;
  773. bytesleft -= mapbytes;
  774. dev_dbg(&pl022->adev->dev,
  775. "set RX/TX target page @ %p, %d bytes, %d left\n",
  776. bufp, mapbytes, bytesleft);
  777. }
  778. } else {
  779. /* Map the dummy buffer on every page */
  780. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  781. if (bytesleft < PAGE_SIZE)
  782. mapbytes = bytesleft;
  783. else
  784. mapbytes = PAGE_SIZE;
  785. sg_set_page(sg, virt_to_page(pl022->dummypage),
  786. mapbytes, 0);
  787. bytesleft -= mapbytes;
  788. dev_dbg(&pl022->adev->dev,
  789. "set RX/TX to dummy page %d bytes, %d left\n",
  790. mapbytes, bytesleft);
  791. }
  792. }
  793. BUG_ON(bytesleft);
  794. }
  795. /**
  796. * configure_dma - configures the channels for the next transfer
  797. * @pl022: SSP driver's private data structure
  798. */
  799. static int configure_dma(struct pl022 *pl022)
  800. {
  801. struct dma_slave_config rx_conf = {
  802. .src_addr = SSP_DR(pl022->phybase),
  803. .direction = DMA_DEV_TO_MEM,
  804. .device_fc = false,
  805. };
  806. struct dma_slave_config tx_conf = {
  807. .dst_addr = SSP_DR(pl022->phybase),
  808. .direction = DMA_MEM_TO_DEV,
  809. .device_fc = false,
  810. };
  811. unsigned int pages;
  812. int ret;
  813. int rx_sglen, tx_sglen;
  814. struct dma_chan *rxchan = pl022->dma_rx_channel;
  815. struct dma_chan *txchan = pl022->dma_tx_channel;
  816. struct dma_async_tx_descriptor *rxdesc;
  817. struct dma_async_tx_descriptor *txdesc;
  818. /* Check that the channels are available */
  819. if (!rxchan || !txchan)
  820. return -ENODEV;
  821. /*
  822. * If supplied, the DMA burstsize should equal the FIFO trigger level.
  823. * Notice that the DMA engine uses one-to-one mapping. Since we can
  824. * not trigger on 2 elements this needs explicit mapping rather than
  825. * calculation.
  826. */
  827. switch (pl022->rx_lev_trig) {
  828. case SSP_RX_1_OR_MORE_ELEM:
  829. rx_conf.src_maxburst = 1;
  830. break;
  831. case SSP_RX_4_OR_MORE_ELEM:
  832. rx_conf.src_maxburst = 4;
  833. break;
  834. case SSP_RX_8_OR_MORE_ELEM:
  835. rx_conf.src_maxburst = 8;
  836. break;
  837. case SSP_RX_16_OR_MORE_ELEM:
  838. rx_conf.src_maxburst = 16;
  839. break;
  840. case SSP_RX_32_OR_MORE_ELEM:
  841. rx_conf.src_maxburst = 32;
  842. break;
  843. default:
  844. rx_conf.src_maxburst = pl022->vendor->fifodepth >> 1;
  845. break;
  846. }
  847. switch (pl022->tx_lev_trig) {
  848. case SSP_TX_1_OR_MORE_EMPTY_LOC:
  849. tx_conf.dst_maxburst = 1;
  850. break;
  851. case SSP_TX_4_OR_MORE_EMPTY_LOC:
  852. tx_conf.dst_maxburst = 4;
  853. break;
  854. case SSP_TX_8_OR_MORE_EMPTY_LOC:
  855. tx_conf.dst_maxburst = 8;
  856. break;
  857. case SSP_TX_16_OR_MORE_EMPTY_LOC:
  858. tx_conf.dst_maxburst = 16;
  859. break;
  860. case SSP_TX_32_OR_MORE_EMPTY_LOC:
  861. tx_conf.dst_maxburst = 32;
  862. break;
  863. default:
  864. tx_conf.dst_maxburst = pl022->vendor->fifodepth >> 1;
  865. break;
  866. }
  867. switch (pl022->read) {
  868. case READING_NULL:
  869. /* Use the same as for writing */
  870. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  871. break;
  872. case READING_U8:
  873. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  874. break;
  875. case READING_U16:
  876. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  877. break;
  878. case READING_U32:
  879. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  880. break;
  881. }
  882. switch (pl022->write) {
  883. case WRITING_NULL:
  884. /* Use the same as for reading */
  885. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  886. break;
  887. case WRITING_U8:
  888. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  889. break;
  890. case WRITING_U16:
  891. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  892. break;
  893. case WRITING_U32:
  894. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  895. break;
  896. }
  897. /* SPI pecularity: we need to read and write the same width */
  898. if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  899. rx_conf.src_addr_width = tx_conf.dst_addr_width;
  900. if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  901. tx_conf.dst_addr_width = rx_conf.src_addr_width;
  902. BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
  903. dmaengine_slave_config(rxchan, &rx_conf);
  904. dmaengine_slave_config(txchan, &tx_conf);
  905. /* Create sglists for the transfers */
  906. pages = DIV_ROUND_UP(pl022->cur_transfer->len, PAGE_SIZE);
  907. dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
  908. ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_ATOMIC);
  909. if (ret)
  910. goto err_alloc_rx_sg;
  911. ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_ATOMIC);
  912. if (ret)
  913. goto err_alloc_tx_sg;
  914. /* Fill in the scatterlists for the RX+TX buffers */
  915. setup_dma_scatter(pl022, pl022->rx,
  916. pl022->cur_transfer->len, &pl022->sgt_rx);
  917. setup_dma_scatter(pl022, pl022->tx,
  918. pl022->cur_transfer->len, &pl022->sgt_tx);
  919. /* Map DMA buffers */
  920. rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  921. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  922. if (!rx_sglen)
  923. goto err_rx_sgmap;
  924. tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  925. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  926. if (!tx_sglen)
  927. goto err_tx_sgmap;
  928. /* Send both scatterlists */
  929. rxdesc = dmaengine_prep_slave_sg(rxchan,
  930. pl022->sgt_rx.sgl,
  931. rx_sglen,
  932. DMA_DEV_TO_MEM,
  933. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  934. if (!rxdesc)
  935. goto err_rxdesc;
  936. txdesc = dmaengine_prep_slave_sg(txchan,
  937. pl022->sgt_tx.sgl,
  938. tx_sglen,
  939. DMA_MEM_TO_DEV,
  940. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  941. if (!txdesc)
  942. goto err_txdesc;
  943. /* Put the callback on the RX transfer only, that should finish last */
  944. rxdesc->callback = dma_callback;
  945. rxdesc->callback_param = pl022;
  946. /* Submit and fire RX and TX with TX last so we're ready to read! */
  947. dmaengine_submit(rxdesc);
  948. dmaengine_submit(txdesc);
  949. dma_async_issue_pending(rxchan);
  950. dma_async_issue_pending(txchan);
  951. pl022->dma_running = true;
  952. return 0;
  953. err_txdesc:
  954. dmaengine_terminate_all(txchan);
  955. err_rxdesc:
  956. dmaengine_terminate_all(rxchan);
  957. dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  958. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  959. err_tx_sgmap:
  960. dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  961. pl022->sgt_tx.nents, DMA_FROM_DEVICE);
  962. err_rx_sgmap:
  963. sg_free_table(&pl022->sgt_tx);
  964. err_alloc_tx_sg:
  965. sg_free_table(&pl022->sgt_rx);
  966. err_alloc_rx_sg:
  967. return -ENOMEM;
  968. }
  969. static int __devinit pl022_dma_probe(struct pl022 *pl022)
  970. {
  971. dma_cap_mask_t mask;
  972. /* Try to acquire a generic DMA engine slave channel */
  973. dma_cap_zero(mask);
  974. dma_cap_set(DMA_SLAVE, mask);
  975. /*
  976. * We need both RX and TX channels to do DMA, else do none
  977. * of them.
  978. */
  979. pl022->dma_rx_channel = dma_request_channel(mask,
  980. pl022->master_info->dma_filter,
  981. pl022->master_info->dma_rx_param);
  982. if (!pl022->dma_rx_channel) {
  983. dev_dbg(&pl022->adev->dev, "no RX DMA channel!\n");
  984. goto err_no_rxchan;
  985. }
  986. pl022->dma_tx_channel = dma_request_channel(mask,
  987. pl022->master_info->dma_filter,
  988. pl022->master_info->dma_tx_param);
  989. if (!pl022->dma_tx_channel) {
  990. dev_dbg(&pl022->adev->dev, "no TX DMA channel!\n");
  991. goto err_no_txchan;
  992. }
  993. pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
  994. if (!pl022->dummypage) {
  995. dev_dbg(&pl022->adev->dev, "no DMA dummypage!\n");
  996. goto err_no_dummypage;
  997. }
  998. dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
  999. dma_chan_name(pl022->dma_rx_channel),
  1000. dma_chan_name(pl022->dma_tx_channel));
  1001. return 0;
  1002. err_no_dummypage:
  1003. dma_release_channel(pl022->dma_tx_channel);
  1004. err_no_txchan:
  1005. dma_release_channel(pl022->dma_rx_channel);
  1006. pl022->dma_rx_channel = NULL;
  1007. err_no_rxchan:
  1008. dev_err(&pl022->adev->dev,
  1009. "Failed to work in dma mode, work without dma!\n");
  1010. return -ENODEV;
  1011. }
  1012. static void terminate_dma(struct pl022 *pl022)
  1013. {
  1014. struct dma_chan *rxchan = pl022->dma_rx_channel;
  1015. struct dma_chan *txchan = pl022->dma_tx_channel;
  1016. dmaengine_terminate_all(rxchan);
  1017. dmaengine_terminate_all(txchan);
  1018. unmap_free_dma_scatter(pl022);
  1019. pl022->dma_running = false;
  1020. }
  1021. static void pl022_dma_remove(struct pl022 *pl022)
  1022. {
  1023. if (pl022->dma_running)
  1024. terminate_dma(pl022);
  1025. if (pl022->dma_tx_channel)
  1026. dma_release_channel(pl022->dma_tx_channel);
  1027. if (pl022->dma_rx_channel)
  1028. dma_release_channel(pl022->dma_rx_channel);
  1029. kfree(pl022->dummypage);
  1030. }
  1031. #else
  1032. static inline int configure_dma(struct pl022 *pl022)
  1033. {
  1034. return -ENODEV;
  1035. }
  1036. static inline int pl022_dma_probe(struct pl022 *pl022)
  1037. {
  1038. return 0;
  1039. }
  1040. static inline void pl022_dma_remove(struct pl022 *pl022)
  1041. {
  1042. }
  1043. #endif
  1044. /**
  1045. * pl022_interrupt_handler - Interrupt handler for SSP controller
  1046. *
  1047. * This function handles interrupts generated for an interrupt based transfer.
  1048. * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
  1049. * current message's state as STATE_ERROR and schedule the tasklet
  1050. * pump_transfers which will do the postprocessing of the current message by
  1051. * calling giveback(). Otherwise it reads data from RX FIFO till there is no
  1052. * more data, and writes data in TX FIFO till it is not full. If we complete
  1053. * the transfer we move to the next transfer and schedule the tasklet.
  1054. */
  1055. static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
  1056. {
  1057. struct pl022 *pl022 = dev_id;
  1058. struct spi_message *msg = pl022->cur_msg;
  1059. u16 irq_status = 0;
  1060. u16 flag = 0;
  1061. if (unlikely(!msg)) {
  1062. dev_err(&pl022->adev->dev,
  1063. "bad message state in interrupt handler");
  1064. /* Never fail */
  1065. return IRQ_HANDLED;
  1066. }
  1067. /* Read the Interrupt Status Register */
  1068. irq_status = readw(SSP_MIS(pl022->virtbase));
  1069. if (unlikely(!irq_status))
  1070. return IRQ_NONE;
  1071. /*
  1072. * This handles the FIFO interrupts, the timeout
  1073. * interrupts are flatly ignored, they cannot be
  1074. * trusted.
  1075. */
  1076. if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
  1077. /*
  1078. * Overrun interrupt - bail out since our Data has been
  1079. * corrupted
  1080. */
  1081. dev_err(&pl022->adev->dev, "FIFO overrun\n");
  1082. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
  1083. dev_err(&pl022->adev->dev,
  1084. "RXFIFO is full\n");
  1085. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
  1086. dev_err(&pl022->adev->dev,
  1087. "TXFIFO is full\n");
  1088. /*
  1089. * Disable and clear interrupts, disable SSP,
  1090. * mark message with bad status so it can be
  1091. * retried.
  1092. */
  1093. writew(DISABLE_ALL_INTERRUPTS,
  1094. SSP_IMSC(pl022->virtbase));
  1095. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1096. writew((readw(SSP_CR1(pl022->virtbase)) &
  1097. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1098. msg->state = STATE_ERROR;
  1099. /* Schedule message queue handler */
  1100. tasklet_schedule(&pl022->pump_transfers);
  1101. return IRQ_HANDLED;
  1102. }
  1103. readwriter(pl022);
  1104. if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
  1105. flag = 1;
  1106. /* Disable Transmit interrupt, enable receive interrupt */
  1107. writew((readw(SSP_IMSC(pl022->virtbase)) &
  1108. ~SSP_IMSC_MASK_TXIM) | SSP_IMSC_MASK_RXIM,
  1109. SSP_IMSC(pl022->virtbase));
  1110. }
  1111. /*
  1112. * Since all transactions must write as much as shall be read,
  1113. * we can conclude the entire transaction once RX is complete.
  1114. * At this point, all TX will always be finished.
  1115. */
  1116. if (pl022->rx >= pl022->rx_end) {
  1117. writew(DISABLE_ALL_INTERRUPTS,
  1118. SSP_IMSC(pl022->virtbase));
  1119. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1120. if (unlikely(pl022->rx > pl022->rx_end)) {
  1121. dev_warn(&pl022->adev->dev, "read %u surplus "
  1122. "bytes (did you request an odd "
  1123. "number of bytes on a 16bit bus?)\n",
  1124. (u32) (pl022->rx - pl022->rx_end));
  1125. }
  1126. /* Update total bytes transferred */
  1127. msg->actual_length += pl022->cur_transfer->len;
  1128. if (pl022->cur_transfer->cs_change)
  1129. pl022->cur_chip->
  1130. cs_control(SSP_CHIP_DESELECT);
  1131. /* Move to next transfer */
  1132. msg->state = next_transfer(pl022);
  1133. tasklet_schedule(&pl022->pump_transfers);
  1134. return IRQ_HANDLED;
  1135. }
  1136. return IRQ_HANDLED;
  1137. }
  1138. /**
  1139. * This sets up the pointers to memory for the next message to
  1140. * send out on the SPI bus.
  1141. */
  1142. static int set_up_next_transfer(struct pl022 *pl022,
  1143. struct spi_transfer *transfer)
  1144. {
  1145. int residue;
  1146. /* Sanity check the message for this bus width */
  1147. residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
  1148. if (unlikely(residue != 0)) {
  1149. dev_err(&pl022->adev->dev,
  1150. "message of %u bytes to transmit but the current "
  1151. "chip bus has a data width of %u bytes!\n",
  1152. pl022->cur_transfer->len,
  1153. pl022->cur_chip->n_bytes);
  1154. dev_err(&pl022->adev->dev, "skipping this message\n");
  1155. return -EIO;
  1156. }
  1157. pl022->tx = (void *)transfer->tx_buf;
  1158. pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
  1159. pl022->rx = (void *)transfer->rx_buf;
  1160. pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
  1161. pl022->write =
  1162. pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
  1163. pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
  1164. return 0;
  1165. }
  1166. /**
  1167. * pump_transfers - Tasklet function which schedules next transfer
  1168. * when running in interrupt or DMA transfer mode.
  1169. * @data: SSP driver private data structure
  1170. *
  1171. */
  1172. static void pump_transfers(unsigned long data)
  1173. {
  1174. struct pl022 *pl022 = (struct pl022 *) data;
  1175. struct spi_message *message = NULL;
  1176. struct spi_transfer *transfer = NULL;
  1177. struct spi_transfer *previous = NULL;
  1178. /* Get current state information */
  1179. message = pl022->cur_msg;
  1180. transfer = pl022->cur_transfer;
  1181. /* Handle for abort */
  1182. if (message->state == STATE_ERROR) {
  1183. message->status = -EIO;
  1184. giveback(pl022);
  1185. return;
  1186. }
  1187. /* Handle end of message */
  1188. if (message->state == STATE_DONE) {
  1189. message->status = 0;
  1190. giveback(pl022);
  1191. return;
  1192. }
  1193. /* Delay if requested at end of transfer before CS change */
  1194. if (message->state == STATE_RUNNING) {
  1195. previous = list_entry(transfer->transfer_list.prev,
  1196. struct spi_transfer,
  1197. transfer_list);
  1198. if (previous->delay_usecs)
  1199. /*
  1200. * FIXME: This runs in interrupt context.
  1201. * Is this really smart?
  1202. */
  1203. udelay(previous->delay_usecs);
  1204. /* Reselect chip select only if cs_change was requested */
  1205. if (previous->cs_change)
  1206. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1207. } else {
  1208. /* STATE_START */
  1209. message->state = STATE_RUNNING;
  1210. }
  1211. if (set_up_next_transfer(pl022, transfer)) {
  1212. message->state = STATE_ERROR;
  1213. message->status = -EIO;
  1214. giveback(pl022);
  1215. return;
  1216. }
  1217. /* Flush the FIFOs and let's go! */
  1218. flush(pl022);
  1219. if (pl022->cur_chip->enable_dma) {
  1220. if (configure_dma(pl022)) {
  1221. dev_dbg(&pl022->adev->dev,
  1222. "configuration of DMA failed, fall back to interrupt mode\n");
  1223. goto err_config_dma;
  1224. }
  1225. return;
  1226. }
  1227. err_config_dma:
  1228. /* enable all interrupts except RX */
  1229. writew(ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM, SSP_IMSC(pl022->virtbase));
  1230. }
  1231. static void do_interrupt_dma_transfer(struct pl022 *pl022)
  1232. {
  1233. /*
  1234. * Default is to enable all interrupts except RX -
  1235. * this will be enabled once TX is complete
  1236. */
  1237. u32 irqflags = ENABLE_ALL_INTERRUPTS & ~SSP_IMSC_MASK_RXIM;
  1238. /* Enable target chip, if not already active */
  1239. if (!pl022->next_msg_cs_active)
  1240. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1241. if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
  1242. /* Error path */
  1243. pl022->cur_msg->state = STATE_ERROR;
  1244. pl022->cur_msg->status = -EIO;
  1245. giveback(pl022);
  1246. return;
  1247. }
  1248. /* If we're using DMA, set up DMA here */
  1249. if (pl022->cur_chip->enable_dma) {
  1250. /* Configure DMA transfer */
  1251. if (configure_dma(pl022)) {
  1252. dev_dbg(&pl022->adev->dev,
  1253. "configuration of DMA failed, fall back to interrupt mode\n");
  1254. goto err_config_dma;
  1255. }
  1256. /* Disable interrupts in DMA mode, IRQ from DMA controller */
  1257. irqflags = DISABLE_ALL_INTERRUPTS;
  1258. }
  1259. err_config_dma:
  1260. /* Enable SSP, turn on interrupts */
  1261. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1262. SSP_CR1(pl022->virtbase));
  1263. writew(irqflags, SSP_IMSC(pl022->virtbase));
  1264. }
  1265. static void do_polling_transfer(struct pl022 *pl022)
  1266. {
  1267. struct spi_message *message = NULL;
  1268. struct spi_transfer *transfer = NULL;
  1269. struct spi_transfer *previous = NULL;
  1270. struct chip_data *chip;
  1271. unsigned long time, timeout;
  1272. chip = pl022->cur_chip;
  1273. message = pl022->cur_msg;
  1274. while (message->state != STATE_DONE) {
  1275. /* Handle for abort */
  1276. if (message->state == STATE_ERROR)
  1277. break;
  1278. transfer = pl022->cur_transfer;
  1279. /* Delay if requested at end of transfer */
  1280. if (message->state == STATE_RUNNING) {
  1281. previous =
  1282. list_entry(transfer->transfer_list.prev,
  1283. struct spi_transfer, transfer_list);
  1284. if (previous->delay_usecs)
  1285. udelay(previous->delay_usecs);
  1286. if (previous->cs_change)
  1287. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1288. } else {
  1289. /* STATE_START */
  1290. message->state = STATE_RUNNING;
  1291. if (!pl022->next_msg_cs_active)
  1292. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1293. }
  1294. /* Configuration Changing Per Transfer */
  1295. if (set_up_next_transfer(pl022, transfer)) {
  1296. /* Error path */
  1297. message->state = STATE_ERROR;
  1298. break;
  1299. }
  1300. /* Flush FIFOs and enable SSP */
  1301. flush(pl022);
  1302. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1303. SSP_CR1(pl022->virtbase));
  1304. dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
  1305. timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT);
  1306. while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end) {
  1307. time = jiffies;
  1308. readwriter(pl022);
  1309. if (time_after(time, timeout)) {
  1310. dev_warn(&pl022->adev->dev,
  1311. "%s: timeout!\n", __func__);
  1312. message->state = STATE_ERROR;
  1313. goto out;
  1314. }
  1315. cpu_relax();
  1316. }
  1317. /* Update total byte transferred */
  1318. message->actual_length += pl022->cur_transfer->len;
  1319. if (pl022->cur_transfer->cs_change)
  1320. pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
  1321. /* Move to next transfer */
  1322. message->state = next_transfer(pl022);
  1323. }
  1324. out:
  1325. /* Handle end of message */
  1326. if (message->state == STATE_DONE)
  1327. message->status = 0;
  1328. else
  1329. message->status = -EIO;
  1330. giveback(pl022);
  1331. return;
  1332. }
  1333. static int pl022_transfer_one_message(struct spi_master *master,
  1334. struct spi_message *msg)
  1335. {
  1336. struct pl022 *pl022 = spi_master_get_devdata(master);
  1337. /* Initial message state */
  1338. pl022->cur_msg = msg;
  1339. msg->state = STATE_START;
  1340. pl022->cur_transfer = list_entry(msg->transfers.next,
  1341. struct spi_transfer, transfer_list);
  1342. /* Setup the SPI using the per chip configuration */
  1343. pl022->cur_chip = spi_get_ctldata(msg->spi);
  1344. restore_state(pl022);
  1345. flush(pl022);
  1346. if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
  1347. do_polling_transfer(pl022);
  1348. else
  1349. do_interrupt_dma_transfer(pl022);
  1350. return 0;
  1351. }
  1352. static int pl022_prepare_transfer_hardware(struct spi_master *master)
  1353. {
  1354. struct pl022 *pl022 = spi_master_get_devdata(master);
  1355. /*
  1356. * Just make sure we have all we need to run the transfer by syncing
  1357. * with the runtime PM framework.
  1358. */
  1359. pm_runtime_get_sync(&pl022->adev->dev);
  1360. return 0;
  1361. }
  1362. static int pl022_unprepare_transfer_hardware(struct spi_master *master)
  1363. {
  1364. struct pl022 *pl022 = spi_master_get_devdata(master);
  1365. /* nothing more to do - disable spi/ssp and power off */
  1366. writew((readw(SSP_CR1(pl022->virtbase)) &
  1367. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1368. if (pl022->master_info->autosuspend_delay > 0) {
  1369. pm_runtime_mark_last_busy(&pl022->adev->dev);
  1370. pm_runtime_put_autosuspend(&pl022->adev->dev);
  1371. } else {
  1372. pm_runtime_put(&pl022->adev->dev);
  1373. }
  1374. return 0;
  1375. }
  1376. static int verify_controller_parameters(struct pl022 *pl022,
  1377. struct pl022_config_chip const *chip_info)
  1378. {
  1379. if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
  1380. || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
  1381. dev_err(&pl022->adev->dev,
  1382. "interface is configured incorrectly\n");
  1383. return -EINVAL;
  1384. }
  1385. if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
  1386. (!pl022->vendor->unidir)) {
  1387. dev_err(&pl022->adev->dev,
  1388. "unidirectional mode not supported in this "
  1389. "hardware version\n");
  1390. return -EINVAL;
  1391. }
  1392. if ((chip_info->hierarchy != SSP_MASTER)
  1393. && (chip_info->hierarchy != SSP_SLAVE)) {
  1394. dev_err(&pl022->adev->dev,
  1395. "hierarchy is configured incorrectly\n");
  1396. return -EINVAL;
  1397. }
  1398. if ((chip_info->com_mode != INTERRUPT_TRANSFER)
  1399. && (chip_info->com_mode != DMA_TRANSFER)
  1400. && (chip_info->com_mode != POLLING_TRANSFER)) {
  1401. dev_err(&pl022->adev->dev,
  1402. "Communication mode is configured incorrectly\n");
  1403. return -EINVAL;
  1404. }
  1405. switch (chip_info->rx_lev_trig) {
  1406. case SSP_RX_1_OR_MORE_ELEM:
  1407. case SSP_RX_4_OR_MORE_ELEM:
  1408. case SSP_RX_8_OR_MORE_ELEM:
  1409. /* These are always OK, all variants can handle this */
  1410. break;
  1411. case SSP_RX_16_OR_MORE_ELEM:
  1412. if (pl022->vendor->fifodepth < 16) {
  1413. dev_err(&pl022->adev->dev,
  1414. "RX FIFO Trigger Level is configured incorrectly\n");
  1415. return -EINVAL;
  1416. }
  1417. break;
  1418. case SSP_RX_32_OR_MORE_ELEM:
  1419. if (pl022->vendor->fifodepth < 32) {
  1420. dev_err(&pl022->adev->dev,
  1421. "RX FIFO Trigger Level is configured incorrectly\n");
  1422. return -EINVAL;
  1423. }
  1424. break;
  1425. default:
  1426. dev_err(&pl022->adev->dev,
  1427. "RX FIFO Trigger Level is configured incorrectly\n");
  1428. return -EINVAL;
  1429. break;
  1430. }
  1431. switch (chip_info->tx_lev_trig) {
  1432. case SSP_TX_1_OR_MORE_EMPTY_LOC:
  1433. case SSP_TX_4_OR_MORE_EMPTY_LOC:
  1434. case SSP_TX_8_OR_MORE_EMPTY_LOC:
  1435. /* These are always OK, all variants can handle this */
  1436. break;
  1437. case SSP_TX_16_OR_MORE_EMPTY_LOC:
  1438. if (pl022->vendor->fifodepth < 16) {
  1439. dev_err(&pl022->adev->dev,
  1440. "TX FIFO Trigger Level is configured incorrectly\n");
  1441. return -EINVAL;
  1442. }
  1443. break;
  1444. case SSP_TX_32_OR_MORE_EMPTY_LOC:
  1445. if (pl022->vendor->fifodepth < 32) {
  1446. dev_err(&pl022->adev->dev,
  1447. "TX FIFO Trigger Level is configured incorrectly\n");
  1448. return -EINVAL;
  1449. }
  1450. break;
  1451. default:
  1452. dev_err(&pl022->adev->dev,
  1453. "TX FIFO Trigger Level is configured incorrectly\n");
  1454. return -EINVAL;
  1455. break;
  1456. }
  1457. if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
  1458. if ((chip_info->ctrl_len < SSP_BITS_4)
  1459. || (chip_info->ctrl_len > SSP_BITS_32)) {
  1460. dev_err(&pl022->adev->dev,
  1461. "CTRL LEN is configured incorrectly\n");
  1462. return -EINVAL;
  1463. }
  1464. if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
  1465. && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
  1466. dev_err(&pl022->adev->dev,
  1467. "Wait State is configured incorrectly\n");
  1468. return -EINVAL;
  1469. }
  1470. /* Half duplex is only available in the ST Micro version */
  1471. if (pl022->vendor->extended_cr) {
  1472. if ((chip_info->duplex !=
  1473. SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1474. && (chip_info->duplex !=
  1475. SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
  1476. dev_err(&pl022->adev->dev,
  1477. "Microwire duplex mode is configured incorrectly\n");
  1478. return -EINVAL;
  1479. }
  1480. } else {
  1481. if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1482. dev_err(&pl022->adev->dev,
  1483. "Microwire half duplex mode requested,"
  1484. " but this is only available in the"
  1485. " ST version of PL022\n");
  1486. return -EINVAL;
  1487. }
  1488. }
  1489. return 0;
  1490. }
  1491. static inline u32 spi_rate(u32 rate, u16 cpsdvsr, u16 scr)
  1492. {
  1493. return rate / (cpsdvsr * (1 + scr));
  1494. }
  1495. static int calculate_effective_freq(struct pl022 *pl022, int freq, struct
  1496. ssp_clock_params * clk_freq)
  1497. {
  1498. /* Lets calculate the frequency parameters */
  1499. u16 cpsdvsr = CPSDVR_MIN, scr = SCR_MIN;
  1500. u32 rate, max_tclk, min_tclk, best_freq = 0, best_cpsdvsr = 0,
  1501. best_scr = 0, tmp, found = 0;
  1502. rate = clk_get_rate(pl022->clk);
  1503. /* cpsdvscr = 2 & scr 0 */
  1504. max_tclk = spi_rate(rate, CPSDVR_MIN, SCR_MIN);
  1505. /* cpsdvsr = 254 & scr = 255 */
  1506. min_tclk = spi_rate(rate, CPSDVR_MAX, SCR_MAX);
  1507. if (!((freq <= max_tclk) && (freq >= min_tclk))) {
  1508. dev_err(&pl022->adev->dev,
  1509. "controller data is incorrect: out of range frequency");
  1510. return -EINVAL;
  1511. }
  1512. /*
  1513. * best_freq will give closest possible available rate (<= requested
  1514. * freq) for all values of scr & cpsdvsr.
  1515. */
  1516. while ((cpsdvsr <= CPSDVR_MAX) && !found) {
  1517. while (scr <= SCR_MAX) {
  1518. tmp = spi_rate(rate, cpsdvsr, scr);
  1519. if (tmp > freq)
  1520. scr++;
  1521. /*
  1522. * If found exact value, update and break.
  1523. * If found more closer value, update and continue.
  1524. */
  1525. else if ((tmp == freq) || (tmp > best_freq)) {
  1526. best_freq = tmp;
  1527. best_cpsdvsr = cpsdvsr;
  1528. best_scr = scr;
  1529. if (tmp == freq)
  1530. break;
  1531. }
  1532. scr++;
  1533. }
  1534. cpsdvsr += 2;
  1535. scr = SCR_MIN;
  1536. }
  1537. clk_freq->cpsdvsr = (u8) (best_cpsdvsr & 0xFF);
  1538. clk_freq->scr = (u8) (best_scr & 0xFF);
  1539. dev_dbg(&pl022->adev->dev,
  1540. "SSP Target Frequency is: %u, Effective Frequency is %u\n",
  1541. freq, best_freq);
  1542. dev_dbg(&pl022->adev->dev, "SSP cpsdvsr = %d, scr = %d\n",
  1543. clk_freq->cpsdvsr, clk_freq->scr);
  1544. return 0;
  1545. }
  1546. /*
  1547. * A piece of default chip info unless the platform
  1548. * supplies it.
  1549. */
  1550. static const struct pl022_config_chip pl022_default_chip_info = {
  1551. .com_mode = POLLING_TRANSFER,
  1552. .iface = SSP_INTERFACE_MOTOROLA_SPI,
  1553. .hierarchy = SSP_SLAVE,
  1554. .slave_tx_disable = DO_NOT_DRIVE_TX,
  1555. .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
  1556. .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
  1557. .ctrl_len = SSP_BITS_8,
  1558. .wait_state = SSP_MWIRE_WAIT_ZERO,
  1559. .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
  1560. .cs_control = null_cs_control,
  1561. };
  1562. /**
  1563. * pl022_setup - setup function registered to SPI master framework
  1564. * @spi: spi device which is requesting setup
  1565. *
  1566. * This function is registered to the SPI framework for this SPI master
  1567. * controller. If it is the first time when setup is called by this device,
  1568. * this function will initialize the runtime state for this chip and save
  1569. * the same in the device structure. Else it will update the runtime info
  1570. * with the updated chip info. Nothing is really being written to the
  1571. * controller hardware here, that is not done until the actual transfer
  1572. * commence.
  1573. */
  1574. static int pl022_setup(struct spi_device *spi)
  1575. {
  1576. struct pl022_config_chip const *chip_info;
  1577. struct chip_data *chip;
  1578. struct ssp_clock_params clk_freq = { .cpsdvsr = 0, .scr = 0};
  1579. int status = 0;
  1580. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1581. unsigned int bits = spi->bits_per_word;
  1582. u32 tmp;
  1583. if (!spi->max_speed_hz)
  1584. return -EINVAL;
  1585. /* Get controller_state if one is supplied */
  1586. chip = spi_get_ctldata(spi);
  1587. if (chip == NULL) {
  1588. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1589. if (!chip) {
  1590. dev_err(&spi->dev,
  1591. "cannot allocate controller state\n");
  1592. return -ENOMEM;
  1593. }
  1594. dev_dbg(&spi->dev,
  1595. "allocated memory for controller's runtime state\n");
  1596. }
  1597. /* Get controller data if one is supplied */
  1598. chip_info = spi->controller_data;
  1599. if (chip_info == NULL) {
  1600. chip_info = &pl022_default_chip_info;
  1601. /* spi_board_info.controller_data not is supplied */
  1602. dev_dbg(&spi->dev,
  1603. "using default controller_data settings\n");
  1604. } else
  1605. dev_dbg(&spi->dev,
  1606. "using user supplied controller_data settings\n");
  1607. /*
  1608. * We can override with custom divisors, else we use the board
  1609. * frequency setting
  1610. */
  1611. if ((0 == chip_info->clk_freq.cpsdvsr)
  1612. && (0 == chip_info->clk_freq.scr)) {
  1613. status = calculate_effective_freq(pl022,
  1614. spi->max_speed_hz,
  1615. &clk_freq);
  1616. if (status < 0)
  1617. goto err_config_params;
  1618. } else {
  1619. memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
  1620. if ((clk_freq.cpsdvsr % 2) != 0)
  1621. clk_freq.cpsdvsr =
  1622. clk_freq.cpsdvsr - 1;
  1623. }
  1624. if ((clk_freq.cpsdvsr < CPSDVR_MIN)
  1625. || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
  1626. status = -EINVAL;
  1627. dev_err(&spi->dev,
  1628. "cpsdvsr is configured incorrectly\n");
  1629. goto err_config_params;
  1630. }
  1631. status = verify_controller_parameters(pl022, chip_info);
  1632. if (status) {
  1633. dev_err(&spi->dev, "controller data is incorrect");
  1634. goto err_config_params;
  1635. }
  1636. pl022->rx_lev_trig = chip_info->rx_lev_trig;
  1637. pl022->tx_lev_trig = chip_info->tx_lev_trig;
  1638. /* Now set controller state based on controller data */
  1639. chip->xfer_type = chip_info->com_mode;
  1640. if (!chip_info->cs_control) {
  1641. chip->cs_control = null_cs_control;
  1642. dev_warn(&spi->dev,
  1643. "chip select function is NULL for this chip\n");
  1644. } else
  1645. chip->cs_control = chip_info->cs_control;
  1646. if (bits <= 3) {
  1647. /* PL022 doesn't support less than 4-bits */
  1648. status = -ENOTSUPP;
  1649. goto err_config_params;
  1650. } else if (bits <= 8) {
  1651. dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
  1652. chip->n_bytes = 1;
  1653. chip->read = READING_U8;
  1654. chip->write = WRITING_U8;
  1655. } else if (bits <= 16) {
  1656. dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
  1657. chip->n_bytes = 2;
  1658. chip->read = READING_U16;
  1659. chip->write = WRITING_U16;
  1660. } else {
  1661. if (pl022->vendor->max_bpw >= 32) {
  1662. dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
  1663. chip->n_bytes = 4;
  1664. chip->read = READING_U32;
  1665. chip->write = WRITING_U32;
  1666. } else {
  1667. dev_err(&spi->dev,
  1668. "illegal data size for this controller!\n");
  1669. dev_err(&spi->dev,
  1670. "a standard pl022 can only handle "
  1671. "1 <= n <= 16 bit words\n");
  1672. status = -ENOTSUPP;
  1673. goto err_config_params;
  1674. }
  1675. }
  1676. /* Now Initialize all register settings required for this chip */
  1677. chip->cr0 = 0;
  1678. chip->cr1 = 0;
  1679. chip->dmacr = 0;
  1680. chip->cpsr = 0;
  1681. if ((chip_info->com_mode == DMA_TRANSFER)
  1682. && ((pl022->master_info)->enable_dma)) {
  1683. chip->enable_dma = true;
  1684. dev_dbg(&spi->dev, "DMA mode set in controller state\n");
  1685. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1686. SSP_DMACR_MASK_RXDMAE, 0);
  1687. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1688. SSP_DMACR_MASK_TXDMAE, 1);
  1689. } else {
  1690. chip->enable_dma = false;
  1691. dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
  1692. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1693. SSP_DMACR_MASK_RXDMAE, 0);
  1694. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1695. SSP_DMACR_MASK_TXDMAE, 1);
  1696. }
  1697. chip->cpsr = clk_freq.cpsdvsr;
  1698. /* Special setup for the ST micro extended control registers */
  1699. if (pl022->vendor->extended_cr) {
  1700. u32 etx;
  1701. if (pl022->vendor->pl023) {
  1702. /* These bits are only in the PL023 */
  1703. SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
  1704. SSP_CR1_MASK_FBCLKDEL_ST, 13);
  1705. } else {
  1706. /* These bits are in the PL022 but not PL023 */
  1707. SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
  1708. SSP_CR0_MASK_HALFDUP_ST, 5);
  1709. SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
  1710. SSP_CR0_MASK_CSS_ST, 16);
  1711. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1712. SSP_CR0_MASK_FRF_ST, 21);
  1713. SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
  1714. SSP_CR1_MASK_MWAIT_ST, 6);
  1715. }
  1716. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1717. SSP_CR0_MASK_DSS_ST, 0);
  1718. if (spi->mode & SPI_LSB_FIRST) {
  1719. tmp = SSP_RX_LSB;
  1720. etx = SSP_TX_LSB;
  1721. } else {
  1722. tmp = SSP_RX_MSB;
  1723. etx = SSP_TX_MSB;
  1724. }
  1725. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
  1726. SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
  1727. SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
  1728. SSP_CR1_MASK_RXIFLSEL_ST, 7);
  1729. SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
  1730. SSP_CR1_MASK_TXIFLSEL_ST, 10);
  1731. } else {
  1732. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1733. SSP_CR0_MASK_DSS, 0);
  1734. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1735. SSP_CR0_MASK_FRF, 4);
  1736. }
  1737. /* Stuff that is common for all versions */
  1738. if (spi->mode & SPI_CPOL)
  1739. tmp = SSP_CLK_POL_IDLE_HIGH;
  1740. else
  1741. tmp = SSP_CLK_POL_IDLE_LOW;
  1742. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
  1743. if (spi->mode & SPI_CPHA)
  1744. tmp = SSP_CLK_SECOND_EDGE;
  1745. else
  1746. tmp = SSP_CLK_FIRST_EDGE;
  1747. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
  1748. SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
  1749. /* Loopback is available on all versions except PL023 */
  1750. if (pl022->vendor->loopback) {
  1751. if (spi->mode & SPI_LOOP)
  1752. tmp = LOOPBACK_ENABLED;
  1753. else
  1754. tmp = LOOPBACK_DISABLED;
  1755. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
  1756. }
  1757. SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
  1758. SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
  1759. SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD,
  1760. 3);
  1761. /* Save controller_state */
  1762. spi_set_ctldata(spi, chip);
  1763. return status;
  1764. err_config_params:
  1765. spi_set_ctldata(spi, NULL);
  1766. kfree(chip);
  1767. return status;
  1768. }
  1769. /**
  1770. * pl022_cleanup - cleanup function registered to SPI master framework
  1771. * @spi: spi device which is requesting cleanup
  1772. *
  1773. * This function is registered to the SPI framework for this SPI master
  1774. * controller. It will free the runtime state of chip.
  1775. */
  1776. static void pl022_cleanup(struct spi_device *spi)
  1777. {
  1778. struct chip_data *chip = spi_get_ctldata(spi);
  1779. spi_set_ctldata(spi, NULL);
  1780. kfree(chip);
  1781. }
  1782. static int __devinit
  1783. pl022_probe(struct amba_device *adev, const struct amba_id *id)
  1784. {
  1785. struct device *dev = &adev->dev;
  1786. struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
  1787. struct spi_master *master;
  1788. struct pl022 *pl022 = NULL; /*Data for this driver */
  1789. int status = 0;
  1790. dev_info(&adev->dev,
  1791. "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
  1792. if (platform_info == NULL) {
  1793. dev_err(&adev->dev, "probe - no platform data supplied\n");
  1794. status = -ENODEV;
  1795. goto err_no_pdata;
  1796. }
  1797. /* Allocate master with space for data */
  1798. master = spi_alloc_master(dev, sizeof(struct pl022));
  1799. if (master == NULL) {
  1800. dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
  1801. status = -ENOMEM;
  1802. goto err_no_master;
  1803. }
  1804. pl022 = spi_master_get_devdata(master);
  1805. pl022->master = master;
  1806. pl022->master_info = platform_info;
  1807. pl022->adev = adev;
  1808. pl022->vendor = id->data;
  1809. /*
  1810. * Bus Number Which has been Assigned to this SSP controller
  1811. * on this board
  1812. */
  1813. master->bus_num = platform_info->bus_id;
  1814. master->num_chipselect = platform_info->num_chipselect;
  1815. master->cleanup = pl022_cleanup;
  1816. master->setup = pl022_setup;
  1817. master->prepare_transfer_hardware = pl022_prepare_transfer_hardware;
  1818. master->transfer_one_message = pl022_transfer_one_message;
  1819. master->unprepare_transfer_hardware = pl022_unprepare_transfer_hardware;
  1820. master->rt = platform_info->rt;
  1821. /*
  1822. * Supports mode 0-3, loopback, and active low CS. Transfers are
  1823. * always MS bit first on the original pl022.
  1824. */
  1825. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1826. if (pl022->vendor->extended_cr)
  1827. master->mode_bits |= SPI_LSB_FIRST;
  1828. dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
  1829. status = amba_request_regions(adev, NULL);
  1830. if (status)
  1831. goto err_no_ioregion;
  1832. pl022->phybase = adev->res.start;
  1833. pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
  1834. if (pl022->virtbase == NULL) {
  1835. status = -ENOMEM;
  1836. goto err_no_ioremap;
  1837. }
  1838. printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
  1839. adev->res.start, pl022->virtbase);
  1840. pl022->clk = clk_get(&adev->dev, NULL);
  1841. if (IS_ERR(pl022->clk)) {
  1842. status = PTR_ERR(pl022->clk);
  1843. dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
  1844. goto err_no_clk;
  1845. }
  1846. status = clk_prepare(pl022->clk);
  1847. if (status) {
  1848. dev_err(&adev->dev, "could not prepare SSP/SPI bus clock\n");
  1849. goto err_clk_prep;
  1850. }
  1851. status = clk_enable(pl022->clk);
  1852. if (status) {
  1853. dev_err(&adev->dev, "could not enable SSP/SPI bus clock\n");
  1854. goto err_no_clk_en;
  1855. }
  1856. /* Initialize transfer pump */
  1857. tasklet_init(&pl022->pump_transfers, pump_transfers,
  1858. (unsigned long)pl022);
  1859. /* Disable SSP */
  1860. writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
  1861. SSP_CR1(pl022->virtbase));
  1862. load_ssp_default_config(pl022);
  1863. status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
  1864. pl022);
  1865. if (status < 0) {
  1866. dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
  1867. goto err_no_irq;
  1868. }
  1869. /* Get DMA channels */
  1870. if (platform_info->enable_dma) {
  1871. status = pl022_dma_probe(pl022);
  1872. if (status != 0)
  1873. platform_info->enable_dma = 0;
  1874. }
  1875. /* Register with the SPI framework */
  1876. amba_set_drvdata(adev, pl022);
  1877. status = spi_register_master(master);
  1878. if (status != 0) {
  1879. dev_err(&adev->dev,
  1880. "probe - problem registering spi master\n");
  1881. goto err_spi_register;
  1882. }
  1883. dev_dbg(dev, "probe succeeded\n");
  1884. /* let runtime pm put suspend */
  1885. if (platform_info->autosuspend_delay > 0) {
  1886. dev_info(&adev->dev,
  1887. "will use autosuspend for runtime pm, delay %dms\n",
  1888. platform_info->autosuspend_delay);
  1889. pm_runtime_set_autosuspend_delay(dev,
  1890. platform_info->autosuspend_delay);
  1891. pm_runtime_use_autosuspend(dev);
  1892. pm_runtime_put_autosuspend(dev);
  1893. } else {
  1894. pm_runtime_put(dev);
  1895. }
  1896. return 0;
  1897. err_spi_register:
  1898. if (platform_info->enable_dma)
  1899. pl022_dma_remove(pl022);
  1900. free_irq(adev->irq[0], pl022);
  1901. err_no_irq:
  1902. clk_disable(pl022->clk);
  1903. err_no_clk_en:
  1904. clk_unprepare(pl022->clk);
  1905. err_clk_prep:
  1906. clk_put(pl022->clk);
  1907. err_no_clk:
  1908. iounmap(pl022->virtbase);
  1909. err_no_ioremap:
  1910. amba_release_regions(adev);
  1911. err_no_ioregion:
  1912. spi_master_put(master);
  1913. err_no_master:
  1914. err_no_pdata:
  1915. return status;
  1916. }
  1917. static int __devexit
  1918. pl022_remove(struct amba_device *adev)
  1919. {
  1920. struct pl022 *pl022 = amba_get_drvdata(adev);
  1921. if (!pl022)
  1922. return 0;
  1923. /*
  1924. * undo pm_runtime_put() in probe. I assume that we're not
  1925. * accessing the primecell here.
  1926. */
  1927. pm_runtime_get_noresume(&adev->dev);
  1928. load_ssp_default_config(pl022);
  1929. if (pl022->master_info->enable_dma)
  1930. pl022_dma_remove(pl022);
  1931. free_irq(adev->irq[0], pl022);
  1932. clk_disable(pl022->clk);
  1933. clk_unprepare(pl022->clk);
  1934. clk_put(pl022->clk);
  1935. iounmap(pl022->virtbase);
  1936. amba_release_regions(adev);
  1937. tasklet_disable(&pl022->pump_transfers);
  1938. spi_unregister_master(pl022->master);
  1939. spi_master_put(pl022->master);
  1940. amba_set_drvdata(adev, NULL);
  1941. return 0;
  1942. }
  1943. #ifdef CONFIG_SUSPEND
  1944. static int pl022_suspend(struct device *dev)
  1945. {
  1946. struct pl022 *pl022 = dev_get_drvdata(dev);
  1947. int ret;
  1948. ret = spi_master_suspend(pl022->master);
  1949. if (ret) {
  1950. dev_warn(dev, "cannot suspend master\n");
  1951. return ret;
  1952. }
  1953. dev_dbg(dev, "suspended\n");
  1954. return 0;
  1955. }
  1956. static int pl022_resume(struct device *dev)
  1957. {
  1958. struct pl022 *pl022 = dev_get_drvdata(dev);
  1959. int ret;
  1960. /* Start the queue running */
  1961. ret = spi_master_resume(pl022->master);
  1962. if (ret)
  1963. dev_err(dev, "problem starting queue (%d)\n", ret);
  1964. else
  1965. dev_dbg(dev, "resumed\n");
  1966. return ret;
  1967. }
  1968. #endif /* CONFIG_PM */
  1969. #ifdef CONFIG_PM_RUNTIME
  1970. static int pl022_runtime_suspend(struct device *dev)
  1971. {
  1972. struct pl022 *pl022 = dev_get_drvdata(dev);
  1973. clk_disable(pl022->clk);
  1974. amba_vcore_disable(pl022->adev);
  1975. return 0;
  1976. }
  1977. static int pl022_runtime_resume(struct device *dev)
  1978. {
  1979. struct pl022 *pl022 = dev_get_drvdata(dev);
  1980. amba_vcore_enable(pl022->adev);
  1981. clk_enable(pl022->clk);
  1982. return 0;
  1983. }
  1984. #endif
  1985. static const struct dev_pm_ops pl022_dev_pm_ops = {
  1986. SET_SYSTEM_SLEEP_PM_OPS(pl022_suspend, pl022_resume)
  1987. SET_RUNTIME_PM_OPS(pl022_runtime_suspend, pl022_runtime_resume, NULL)
  1988. };
  1989. static struct vendor_data vendor_arm = {
  1990. .fifodepth = 8,
  1991. .max_bpw = 16,
  1992. .unidir = false,
  1993. .extended_cr = false,
  1994. .pl023 = false,
  1995. .loopback = true,
  1996. };
  1997. static struct vendor_data vendor_st = {
  1998. .fifodepth = 32,
  1999. .max_bpw = 32,
  2000. .unidir = false,
  2001. .extended_cr = true,
  2002. .pl023 = false,
  2003. .loopback = true,
  2004. };
  2005. static struct vendor_data vendor_st_pl023 = {
  2006. .fifodepth = 32,
  2007. .max_bpw = 32,
  2008. .unidir = false,
  2009. .extended_cr = true,
  2010. .pl023 = true,
  2011. .loopback = false,
  2012. };
  2013. static struct vendor_data vendor_db5500_pl023 = {
  2014. .fifodepth = 32,
  2015. .max_bpw = 32,
  2016. .unidir = false,
  2017. .extended_cr = true,
  2018. .pl023 = true,
  2019. .loopback = true,
  2020. };
  2021. static struct amba_id pl022_ids[] = {
  2022. {
  2023. /*
  2024. * ARM PL022 variant, this has a 16bit wide
  2025. * and 8 locations deep TX/RX FIFO
  2026. */
  2027. .id = 0x00041022,
  2028. .mask = 0x000fffff,
  2029. .data = &vendor_arm,
  2030. },
  2031. {
  2032. /*
  2033. * ST Micro derivative, this has 32bit wide
  2034. * and 32 locations deep TX/RX FIFO
  2035. */
  2036. .id = 0x01080022,
  2037. .mask = 0xffffffff,
  2038. .data = &vendor_st,
  2039. },
  2040. {
  2041. /*
  2042. * ST-Ericsson derivative "PL023" (this is not
  2043. * an official ARM number), this is a PL022 SSP block
  2044. * stripped to SPI mode only, it has 32bit wide
  2045. * and 32 locations deep TX/RX FIFO but no extended
  2046. * CR0/CR1 register
  2047. */
  2048. .id = 0x00080023,
  2049. .mask = 0xffffffff,
  2050. .data = &vendor_st_pl023,
  2051. },
  2052. {
  2053. .id = 0x10080023,
  2054. .mask = 0xffffffff,
  2055. .data = &vendor_db5500_pl023,
  2056. },
  2057. { 0, 0 },
  2058. };
  2059. MODULE_DEVICE_TABLE(amba, pl022_ids);
  2060. static struct amba_driver pl022_driver = {
  2061. .drv = {
  2062. .name = "ssp-pl022",
  2063. .pm = &pl022_dev_pm_ops,
  2064. },
  2065. .id_table = pl022_ids,
  2066. .probe = pl022_probe,
  2067. .remove = __devexit_p(pl022_remove),
  2068. };
  2069. static int __init pl022_init(void)
  2070. {
  2071. return amba_driver_register(&pl022_driver);
  2072. }
  2073. subsys_initcall(pl022_init);
  2074. static void __exit pl022_exit(void)
  2075. {
  2076. amba_driver_unregister(&pl022_driver);
  2077. }
  2078. module_exit(pl022_exit);
  2079. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  2080. MODULE_DESCRIPTION("PL022 SSP Controller Driver");
  2081. MODULE_LICENSE("GPL");