swap_case.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. break;
  128. }
  129. }
  130. return 0;
  131. }
  132. static int sandbox_swap_case_find_bar(struct udevice *emul, unsigned int addr,
  133. int *barnump, unsigned int *offsetp)
  134. {
  135. struct swap_case_platdata *plat = dev_get_platdata(emul);
  136. int barnum;
  137. for (barnum = 0; barnum < ARRAY_SIZE(barinfo); barnum++) {
  138. unsigned int size = barinfo[barnum].size;
  139. if (addr >= plat->bar[barnum] &&
  140. addr < plat->bar[barnum] + size) {
  141. *barnump = barnum;
  142. *offsetp = addr - plat->bar[barnum];
  143. return 0;
  144. }
  145. }
  146. *barnump = -1;
  147. return -ENOENT;
  148. }
  149. static void sandbox_swap_case_do_op(enum swap_case_op op, char *str, int len)
  150. {
  151. for (; len > 0; len--, str++) {
  152. switch (op) {
  153. case OP_TO_UPPER:
  154. *str = toupper(*str);
  155. break;
  156. case OP_TO_LOWER:
  157. *str = tolower(*str);
  158. break;
  159. case OP_SWAP:
  160. if (isupper(*str))
  161. *str = tolower(*str);
  162. else
  163. *str = toupper(*str);
  164. break;
  165. }
  166. }
  167. }
  168. int sandbox_swap_case_read_io(struct udevice *dev, unsigned int addr,
  169. ulong *valuep, enum pci_size_t size)
  170. {
  171. struct swap_case_priv *priv = dev_get_priv(dev);
  172. unsigned int offset;
  173. int barnum;
  174. int ret;
  175. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  176. if (ret)
  177. return ret;
  178. if (barnum == 0 && offset == 0)
  179. *valuep = (*valuep & ~0xff) | priv->op;
  180. return 0;
  181. }
  182. int sandbox_swap_case_write_io(struct udevice *dev, unsigned int addr,
  183. ulong value, enum pci_size_t size)
  184. {
  185. struct swap_case_priv *priv = dev_get_priv(dev);
  186. unsigned int offset;
  187. int barnum;
  188. int ret;
  189. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  190. if (ret)
  191. return ret;
  192. if (barnum == 0 && offset == 0)
  193. priv->op = value;
  194. return 0;
  195. }
  196. static int sandbox_swap_case_map_physmem(struct udevice *dev,
  197. phys_addr_t addr, unsigned long *lenp, void **ptrp)
  198. {
  199. struct swap_case_priv *priv = dev_get_priv(dev);
  200. unsigned int offset, avail;
  201. int barnum;
  202. int ret;
  203. ret = sandbox_swap_case_find_bar(dev, addr, &barnum, &offset);
  204. if (ret)
  205. return ret;
  206. if (barnum == 1) {
  207. *ptrp = priv->mem_text + offset;
  208. avail = barinfo[1].size - offset;
  209. if (avail > barinfo[1].size)
  210. *lenp = 0;
  211. else
  212. *lenp = min(*lenp, (ulong)avail);
  213. return 0;
  214. }
  215. return -ENOENT;
  216. }
  217. static int sandbox_swap_case_unmap_physmem(struct udevice *dev,
  218. const void *vaddr, unsigned long len)
  219. {
  220. struct swap_case_priv *priv = dev_get_priv(dev);
  221. sandbox_swap_case_do_op(priv->op, (void *)vaddr, len);
  222. return 0;
  223. }
  224. struct dm_pci_emul_ops sandbox_swap_case_emul_ops = {
  225. .get_devfn = sandbox_swap_case_get_devfn,
  226. .read_config = sandbox_swap_case_read_config,
  227. .write_config = sandbox_swap_case_write_config,
  228. .read_io = sandbox_swap_case_read_io,
  229. .write_io = sandbox_swap_case_write_io,
  230. .map_physmem = sandbox_swap_case_map_physmem,
  231. .unmap_physmem = sandbox_swap_case_unmap_physmem,
  232. };
  233. static const struct udevice_id sandbox_swap_case_ids[] = {
  234. { .compatible = "sandbox,swap-case" },
  235. { }
  236. };
  237. U_BOOT_DRIVER(sandbox_swap_case_emul) = {
  238. .name = "sandbox_swap_case_emul",
  239. .id = UCLASS_PCI_EMUL,
  240. .of_match = sandbox_swap_case_ids,
  241. .ops = &sandbox_swap_case_emul_ops,
  242. .priv_auto_alloc_size = sizeof(struct swap_case_priv),
  243. .platdata_auto_alloc_size = sizeof(struct swap_case_platdata),
  244. };