swap_case.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI emulation device which swaps the case of text
  4. *
  5. * Copyright (c) 2014 Google, Inc
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <pci.h>
  12. #include <asm/test.h>
  13. #include <linux/ctype.h>
  14. /**
  15. * struct swap_case_platdata - platform data for this device
  16. *
  17. * @command: Current PCI command value
  18. * @bar: Current base address values
  19. */
  20. struct swap_case_platdata {
  21. u16 command;
  22. u32 bar[6];
  23. };
  24. #define offset_to_barnum(offset) \
  25. (((offset) - PCI_BASE_ADDRESS_0) / sizeof(u32))
  26. enum {
  27. MEM_TEXT_SIZE = 0x100,
  28. };
  29. enum swap_case_op {
  30. OP_TO_LOWER,
  31. OP_TO_UPPER,
  32. OP_SWAP,
  33. };
  34. static struct pci_bar {
  35. int type;
  36. u32 size;
  37. } barinfo[] = {
  38. { PCI_BASE_ADDRESS_SPACE_IO, 1 },
  39. { PCI_BASE_ADDRESS_MEM_TYPE_32, MEM_TEXT_SIZE },
  40. { 0, 0 },
  41. { 0, 0 },
  42. { 0, 0 },
  43. { 0, 0 },
  44. };
  45. struct swap_case_priv {
  46. enum swap_case_op op;
  47. char mem_text[MEM_TEXT_SIZE];
  48. };
  49. static int sandbox_swap_case_get_devfn(struct udevice *dev)
  50. {
  51. struct pci_child_platdata *plat = dev_get_parent_platdata(dev);
  52. return plat->devfn;
  53. }
  54. static int sandbox_swap_case_read_config(struct udevice *emul, uint offset,
  55. ulong *valuep, enum pci_size_t size)
  56. {
  57. struct swap_case_platdata *plat = dev_get_platdata(emul);
  58. switch (offset) {
  59. case PCI_COMMAND:
  60. *valuep = plat->command;
  61. break;
  62. case PCI_HEADER_TYPE:
  63. *valuep = 0;
  64. break;
  65. case PCI_VENDOR_ID:
  66. *valuep = SANDBOX_PCI_VENDOR_ID;
  67. break;
  68. case PCI_DEVICE_ID:
  69. *valuep = SANDBOX_PCI_DEVICE_ID;
  70. break;
  71. case PCI_CLASS_DEVICE:
  72. if (size == PCI_SIZE_8) {
  73. *valuep = SANDBOX_PCI_CLASS_SUB_CODE;
  74. } else {
  75. *valuep = (SANDBOX_PCI_CLASS_CODE << 8) |
  76. SANDBOX_PCI_CLASS_SUB_CODE;
  77. }
  78. break;
  79. case PCI_CLASS_CODE:
  80. *valuep = SANDBOX_PCI_CLASS_CODE;
  81. break;
  82. case PCI_BASE_ADDRESS_0:
  83. case PCI_BASE_ADDRESS_1:
  84. case PCI_BASE_ADDRESS_2:
  85. case PCI_BASE_ADDRESS_3:
  86. case PCI_BASE_ADDRESS_4:
  87. case PCI_BASE_ADDRESS_5: {
  88. int barnum;
  89. u32 *bar, result;
  90. barnum = offset_to_barnum(offset);
  91. bar = &plat->bar[barnum];
  92. result = *bar;
  93. if (*bar == 0xffffffff) {
  94. if (barinfo[barnum].type) {
  95. result = (~(barinfo[barnum].size - 1) &
  96. PCI_BASE_ADDRESS_IO_MASK) |
  97. PCI_BASE_ADDRESS_SPACE_IO;
  98. } else {
  99. result = (~(barinfo[barnum].size - 1) &
  100. PCI_BASE_ADDRESS_MEM_MASK) |
  101. PCI_BASE_ADDRESS_MEM_TYPE_32;
  102. }
  103. }
  104. debug("r bar %d=%x\n", barnum, result);
  105. *valuep = result;
  106. break;
  107. }
  108. }
  109. return 0;
  110. }
  111. static int sandbox_swap_case_write_config(struct udevice *emul, uint offset,
  112. ulong value, enum pci_size_t size)
  113. {
  114. struct swap_case_platdata *plat = dev_get_platdata(emul);
  115. switch (offset) {
  116. case PCI_COMMAND:
  117. plat->command = value;
  118. break;
  119. case PCI_BASE_ADDRESS_0:
  120. case PCI_BASE_ADDRESS_1: {
  121. int barnum;
  122. u32 *bar;
  123. barnum = offset_to_barnum(offset);
  124. bar = &plat->bar[barnum];
  125. debug("w bar %d=%lx\n", barnum, value);
  126. *bar = value;
  127. /* space indicator (bit#0) is read-only */
  128. *bar |= barinfo[barnum].type;
  129. break;
  130. }
  131. }
  132. return 0;
  133. }
  134. static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
  135. int *barnump, unsigned int *offsetp)
  136. {
  137. struct swap_case_platdata *plat = dev_get_platdata(emul);
  138. int barnum;
  139. for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
  140. unsigned int size = barinfo[barnum].size;
  141. u32 base = plat->bar[barnum] & ~PCI_BASE_ADDRESS_SPACE;
  142. if (addr >= base && addr < base + size) {
  143. *barnump = barnum;
  144. *offsetp = addr - base;
  145. return 0;
  146. }
  147. }
  148. *barnump = -1;
  149. return -ENOENT;
  150. }
  151. static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
  152. {
  153. for (; len > 0; len--, str++) {
  154. switch (op) {
  155. case OP_TO_UPPER:
  156. *str = toupper(*str);
  157. break;
  158. case OP_TO_LOWER:
  159. *str = tolower(*str);
  160. break;
  161. case OP_SWAP:
  162. if (isupper(*str))
  163. *str = tolower(*str);
  164. else
  165. *str = toupper(*str);
  166. break;
  167. }
  168. }
  169. }
  170. int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
  171. ulong *valuep, enum pci_size_t size)
  172. {
  173. struct swap_case_priv *priv = dev_get_priv(dev);
  174. unsigned int offset;
  175. int barnum;
  176. int ret;
  177. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  178. if (ret)
  179. return ret;
  180. if (barnum == 0 && offset == 0)
  181. *valuep = (*valuep & ~0xff) | priv->op;
  182. return 0;
  183. }
  184. int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
  185. ulong value, enum pci_size_t size)
  186. {
  187. struct swap_case_priv *priv = dev_get_priv(dev);
  188. unsigned int offset;
  189. int barnum;
  190. int ret;
  191. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  192. if (ret)
  193. return ret;
  194. if (barnum == 0 && offset == 0)
  195. priv->op = value;
  196. return 0;
  197. }
  198. static int sandbox_swap_case_map_physmem(struct udevice *dev,
  199. phys_addr_t addr, unsigned long *lenp, void **ptrp)
  200. {
  201. struct swap_case_priv *priv = dev_get_priv(dev);
  202. unsigned int offset, avail;
  203. int barnum;
  204. int ret;
  205. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  206. if (ret)
  207. return ret;
  208. if (barnum == 1) {
  209. *ptrp = priv->mem_text + offset;
  210. avail = barinfo[1].size - offset;
  211. if (avail > barinfo[1].size)
  212. *lenp = 0;
  213. else
  214. *lenp = min(*lenp, (ulong)avail);
  215. return 0;
  216. }
  217. return -ENOENT;
  218. }
  219. static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
  220. const void *vaddr, unsigned long len)
  221. {
  222. struct swap_case_priv *priv = dev_get_priv(dev);
  223. sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
  224. return 0;
  225. }
  226. struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
  227. .get_devfn = sandbox_swap_case_get_devfn,
  228. .read_config = sandbox_swap_case_read_config,
  229. .write_config = sandbox_swap_case_write_config,
  230. .read_io = sandbox_swap_case_read_io,
  231. .write_io = sandbox_swap_case_write_io,
  232. .map_physmem = sandbox_swap_case_map_physmem,
  233. .unmap_physmem = sandbox_swap_case_unmap_physmem,
  234. };
  235. static const struct udevice_id sandbox_swap_case_ids[] = {
  236. { .compatible = "sandbox,swap-case" },
  237. { }
  238. };
  239. U_BOOT_DRIVER(sandbox_swap_case_emul) = {
  240. .name = "sandbox_swap_case_emul",
  241. .id = UCLASS_PCI_EMUL,
  242. .of_match = sandbox_swap_case_ids,
  243. .ops = &sandbox_swap_case_emul_ops,
  244. .priv_auto_alloc_size = sizeof(struct swap_case_priv),
  245. .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),
  246. };