aic94xx_reg.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Aic94xx SAS/SATA driver register access.
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <[email protected]>
  7. */
  8. #include <linux/pci.h>
  9. #include "aic94xx_reg.h"
  10. #include "aic94xx.h"
  11. /* Writing to device address space.
  12. * Offset comes before value to remind that the operation of
  13. * this function is *offs = val.
  14. */
  15. static void asd_write_byte(struct asd_ha_struct *asd_ha,
  16. unsigned long offs, u8 val)
  17. {
  18. if (unlikely(asd_ha->iospace))
  19. outb(val,
  20. (unsigned long)asd_ha->io_handle[0].addr + (offs & 0xFF));
  21. else
  22. writeb(val, asd_ha->io_handle[0].addr + offs);
  23. wmb();
  24. }
  25. static void asd_write_word(struct asd_ha_struct *asd_ha,
  26. unsigned long offs, u16 val)
  27. {
  28. if (unlikely(asd_ha->iospace))
  29. outw(val,
  30. (unsigned long)asd_ha->io_handle[0].addr + (offs & 0xFF));
  31. else
  32. writew(val, asd_ha->io_handle[0].addr + offs);
  33. wmb();
  34. }
  35. static void asd_write_dword(struct asd_ha_struct *asd_ha,
  36. unsigned long offs, u32 val)
  37. {
  38. if (unlikely(asd_ha->iospace))
  39. outl(val,
  40. (unsigned long)asd_ha->io_handle[0].addr + (offs & 0xFF));
  41. else
  42. writel(val, asd_ha->io_handle[0].addr + offs);
  43. wmb();
  44. }
  45. /* Reading from device address space.
  46. */
  47. static u8 asd_read_byte(struct asd_ha_struct *asd_ha, unsigned long offs)
  48. {
  49. u8 val;
  50. if (unlikely(asd_ha->iospace))
  51. val = inb((unsigned long) asd_ha->io_handle[0].addr
  52. + (offs & 0xFF));
  53. else
  54. val = readb(asd_ha->io_handle[0].addr + offs);
  55. rmb();
  56. return val;
  57. }
  58. static u16 asd_read_word(struct asd_ha_struct *asd_ha,
  59. unsigned long offs)
  60. {
  61. u16 val;
  62. if (unlikely(asd_ha->iospace))
  63. val = inw((unsigned long)asd_ha->io_handle[0].addr
  64. + (offs & 0xFF));
  65. else
  66. val = readw(asd_ha->io_handle[0].addr + offs);
  67. rmb();
  68. return val;
  69. }
  70. static u32 asd_read_dword(struct asd_ha_struct *asd_ha,
  71. unsigned long offs)
  72. {
  73. u32 val;
  74. if (unlikely(asd_ha->iospace))
  75. val = inl((unsigned long) asd_ha->io_handle[0].addr
  76. + (offs & 0xFF));
  77. else
  78. val = readl(asd_ha->io_handle[0].addr + offs);
  79. rmb();
  80. return val;
  81. }
  82. static inline u32 asd_mem_offs_swa(void)
  83. {
  84. return 0;
  85. }
  86. static inline u32 asd_mem_offs_swc(void)
  87. {
  88. return asd_mem_offs_swa() + MBAR0_SWA_SIZE;
  89. }
  90. static inline u32 asd_mem_offs_swb(void)
  91. {
  92. return asd_mem_offs_swc() + MBAR0_SWC_SIZE + 0x20;
  93. }
  94. /* We know that the register wanted is in the range
  95. * of the sliding window.
  96. */
  97. #define ASD_READ_SW(ww, type, ord) \
  98. static type asd_read_##ww##_##ord(struct asd_ha_struct *asd_ha, \
  99. u32 reg) \
  100. { \
  101. struct asd_ha_addrspace *io_handle = &asd_ha->io_handle[0]; \
  102. u32 map_offs = (reg - io_handle->ww##_base) + asd_mem_offs_##ww();\
  103. return asd_read_##ord(asd_ha, (unsigned long)map_offs); \
  104. }
  105. #define ASD_WRITE_SW(ww, type, ord) \
  106. static void asd_write_##ww##_##ord(struct asd_ha_struct *asd_ha, \
  107. u32 reg, type val) \
  108. { \
  109. struct asd_ha_addrspace *io_handle = &asd_ha->io_handle[0]; \
  110. u32 map_offs = (reg - io_handle->ww##_base) + asd_mem_offs_##ww();\
  111. asd_write_##ord(asd_ha, (unsigned long)map_offs, val); \
  112. }
  113. ASD_READ_SW(swa, u8, byte);
  114. ASD_READ_SW(swa, u16, word);
  115. ASD_READ_SW(swa, u32, dword);
  116. ASD_READ_SW(swb, u8, byte);
  117. ASD_READ_SW(swb, u16, word);
  118. ASD_READ_SW(swb, u32, dword);
  119. ASD_READ_SW(swc, u8, byte);
  120. ASD_READ_SW(swc, u16, word);
  121. ASD_READ_SW(swc, u32, dword);
  122. ASD_WRITE_SW(swa, u8, byte);
  123. ASD_WRITE_SW(swa, u16, word);
  124. ASD_WRITE_SW(swa, u32, dword);
  125. ASD_WRITE_SW(swb, u8, byte);
  126. ASD_WRITE_SW(swb, u16, word);
  127. ASD_WRITE_SW(swb, u32, dword);
  128. ASD_WRITE_SW(swc, u8, byte);
  129. ASD_WRITE_SW(swc, u16, word);
  130. ASD_WRITE_SW(swc, u32, dword);
  131. /*
  132. * A word about sliding windows:
  133. * MBAR0 is divided into sliding windows A, C and B, in that order.
  134. * SWA starts at offset 0 of MBAR0, up to 0x57, with size 0x58 bytes.
  135. * SWC starts at offset 0x58 of MBAR0, up to 0x60, with size 0x8 bytes.
  136. * From 0x60 to 0x7F, we have a copy of PCI config space 0x60-0x7F.
  137. * SWB starts at offset 0x80 of MBAR0 and extends to the end of MBAR0.
  138. * See asd_init_sw() in aic94xx_hwi.c
  139. *
  140. * We map the most common registers we'd access of the internal 4GB
  141. * host adapter memory space. If a register/internal memory location
  142. * is wanted which is not mapped, we slide SWB, by paging it,
  143. * see asd_move_swb() in aic94xx_reg.c.
  144. */
  145. /**
  146. * asd_move_swb -- move sliding window B
  147. * @asd_ha: pointer to host adapter structure
  148. * @reg: register desired to be within range of the new window
  149. */
  150. static void asd_move_swb(struct asd_ha_struct *asd_ha, u32 reg)
  151. {
  152. u32 base = reg & ~(MBAR0_SWB_SIZE-1);
  153. pci_write_config_dword(asd_ha->pcidev, PCI_CONF_MBAR0_SWB, base);
  154. asd_ha->io_handle[0].swb_base = base;
  155. }
  156. static void __asd_write_reg_byte(struct asd_ha_struct *asd_ha, u32 reg, u8 val)
  157. {
  158. struct asd_ha_addrspace *io_handle=&asd_ha->io_handle[0];
  159. BUG_ON(reg >= 0xC0000000 || reg < ALL_BASE_ADDR);
  160. if (io_handle->swa_base <= reg
  161. && reg < io_handle->swa_base + MBAR0_SWA_SIZE)
  162. asd_write_swa_byte (asd_ha, reg,val);
  163. else if (io_handle->swb_base <= reg
  164. && reg < io_handle->swb_base + MBAR0_SWB_SIZE)
  165. asd_write_swb_byte (asd_ha, reg, val);
  166. else if (io_handle->swc_base <= reg
  167. && reg < io_handle->swc_base + MBAR0_SWC_SIZE)
  168. asd_write_swc_byte (asd_ha, reg, val);
  169. else {
  170. /* Ok, we have to move SWB */
  171. asd_move_swb(asd_ha, reg);
  172. asd_write_swb_byte (asd_ha, reg, val);
  173. }
  174. }
  175. #define ASD_WRITE_REG(type, ord) \
  176. void asd_write_reg_##ord (struct asd_ha_struct *asd_ha, u32 reg, type val)\
  177. { \
  178. struct asd_ha_addrspace *io_handle=&asd_ha->io_handle[0]; \
  179. unsigned long flags; \
  180. BUG_ON(reg >= 0xC0000000 || reg < ALL_BASE_ADDR); \
  181. spin_lock_irqsave(&asd_ha->iolock, flags); \
  182. if (io_handle->swa_base <= reg \
  183. && reg < io_handle->swa_base + MBAR0_SWA_SIZE) \
  184. asd_write_swa_##ord (asd_ha, reg,val); \
  185. else if (io_handle->swb_base <= reg \
  186. && reg < io_handle->swb_base + MBAR0_SWB_SIZE) \
  187. asd_write_swb_##ord (asd_ha, reg, val); \
  188. else if (io_handle->swc_base <= reg \
  189. && reg < io_handle->swc_base + MBAR0_SWC_SIZE) \
  190. asd_write_swc_##ord (asd_ha, reg, val); \
  191. else { \
  192. /* Ok, we have to move SWB */ \
  193. asd_move_swb(asd_ha, reg); \
  194. asd_write_swb_##ord (asd_ha, reg, val); \
  195. } \
  196. spin_unlock_irqrestore(&asd_ha->iolock, flags); \
  197. }
  198. ASD_WRITE_REG(u8, byte);
  199. ASD_WRITE_REG(u16,word);
  200. ASD_WRITE_REG(u32,dword);
  201. static u8 __asd_read_reg_byte(struct asd_ha_struct *asd_ha, u32 reg)
  202. {
  203. struct asd_ha_addrspace *io_handle=&asd_ha->io_handle[0];
  204. u8 val;
  205. BUG_ON(reg >= 0xC0000000 || reg < ALL_BASE_ADDR);
  206. if (io_handle->swa_base <= reg
  207. && reg < io_handle->swa_base + MBAR0_SWA_SIZE)
  208. val = asd_read_swa_byte (asd_ha, reg);
  209. else if (io_handle->swb_base <= reg
  210. && reg < io_handle->swb_base + MBAR0_SWB_SIZE)
  211. val = asd_read_swb_byte (asd_ha, reg);
  212. else if (io_handle->swc_base <= reg
  213. && reg < io_handle->swc_base + MBAR0_SWC_SIZE)
  214. val = asd_read_swc_byte (asd_ha, reg);
  215. else {
  216. /* Ok, we have to move SWB */
  217. asd_move_swb(asd_ha, reg);
  218. val = asd_read_swb_byte (asd_ha, reg);
  219. }
  220. return val;
  221. }
  222. #define ASD_READ_REG(type, ord) \
  223. type asd_read_reg_##ord (struct asd_ha_struct *asd_ha, u32 reg) \
  224. { \
  225. struct asd_ha_addrspace *io_handle=&asd_ha->io_handle[0]; \
  226. type val; \
  227. unsigned long flags; \
  228. BUG_ON(reg >= 0xC0000000 || reg < ALL_BASE_ADDR); \
  229. spin_lock_irqsave(&asd_ha->iolock, flags); \
  230. if (io_handle->swa_base <= reg \
  231. && reg < io_handle->swa_base + MBAR0_SWA_SIZE) \
  232. val = asd_read_swa_##ord (asd_ha, reg); \
  233. else if (io_handle->swb_base <= reg \
  234. && reg < io_handle->swb_base + MBAR0_SWB_SIZE) \
  235. val = asd_read_swb_##ord (asd_ha, reg); \
  236. else if (io_handle->swc_base <= reg \
  237. && reg < io_handle->swc_base + MBAR0_SWC_SIZE) \
  238. val = asd_read_swc_##ord (asd_ha, reg); \
  239. else { \
  240. /* Ok, we have to move SWB */ \
  241. asd_move_swb(asd_ha, reg); \
  242. val = asd_read_swb_##ord (asd_ha, reg); \
  243. } \
  244. spin_unlock_irqrestore(&asd_ha->iolock, flags); \
  245. return val; \
  246. }
  247. ASD_READ_REG(u8, byte);
  248. ASD_READ_REG(u16,word);
  249. ASD_READ_REG(u32,dword);
  250. /**
  251. * asd_read_reg_string -- read a string of bytes from io space memory
  252. * @asd_ha: pointer to host adapter structure
  253. * @dst: pointer to a destination buffer where data will be written to
  254. * @offs: start offset (register) to read from
  255. * @count: number of bytes to read
  256. */
  257. void asd_read_reg_string(struct asd_ha_struct *asd_ha, void *dst,
  258. u32 offs, int count)
  259. {
  260. u8 *p = dst;
  261. unsigned long flags;
  262. spin_lock_irqsave(&asd_ha->iolock, flags);
  263. for ( ; count > 0; count--, offs++, p++)
  264. *p = __asd_read_reg_byte(asd_ha, offs);
  265. spin_unlock_irqrestore(&asd_ha->iolock, flags);
  266. }
  267. /**
  268. * asd_write_reg_string -- write a string of bytes to io space memory
  269. * @asd_ha: pointer to host adapter structure
  270. * @src: pointer to source buffer where data will be read from
  271. * @offs: start offset (register) to write to
  272. * @count: number of bytes to write
  273. */
  274. void asd_write_reg_string(struct asd_ha_struct *asd_ha, void *src,
  275. u32 offs, int count)
  276. {
  277. u8 *p = src;
  278. unsigned long flags;
  279. spin_lock_irqsave(&asd_ha->iolock, flags);
  280. for ( ; count > 0; count--, offs++, p++)
  281. __asd_write_reg_byte(asd_ha, offs, *p);
  282. spin_unlock_irqrestore(&asd_ha->iolock, flags);
  283. }