amba-pl022.c 64 KB

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