isa_dma.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * isa_dma.c
  3. *
  4. * Copyright (C) 2013 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <isa_dma.h>
  20. #include <lock.h>
  21. static dword_t isa_dma_bitmap[1 + ((ISA_DMA_MEM_BLOCKS - 1) / 32)] = { 0 };
  22. static DECLARE_LOCK(dma_lock);
  23. void *isa_dma_alloc(void)
  24. {
  25. dword_t i;
  26. for (i = 0; i < ISA_DMA_MEM_BLOCKS; i++) if (!test_bit(isa_dma_bitmap, i))
  27. {
  28. set_bit(isa_dma_bitmap, i);
  29. return (void*)(ISA_DMA_MEM_START + (i * ISA_DMA_MEM_ALIGNMENT));
  30. }
  31. return NULL;
  32. }
  33. void isa_dma_free(void *dma_buffer)
  34. {
  35. clear_bit(isa_dma_bitmap, ((dword_t)dma_buffer - ISA_DMA_MEM_START) / ISA_DMA_MEM_ALIGNMENT);
  36. }
  37. void isa_dma_read(dword_t channel, void *buffer, word_t count)
  38. {
  39. channel &= 7;
  40. if (count == 0) return;
  41. else count--;
  42. lock_acquire(&dma_lock);
  43. cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), ISA_DMA_MASK_ON | (channel & 3));
  44. cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
  45. cpu_write_port_byte(ISA_DMA_SAR(channel), (dword_t)buffer & 0xFF);
  46. cpu_write_port_byte(ISA_DMA_SAR(channel), ((dword_t)buffer >> 8) & 0xFF);
  47. cpu_write_port_byte(ISA_DMA_PAR(channel), ((dword_t)buffer >> 16) & 0xFF);
  48. cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
  49. cpu_write_port_byte(ISA_DMA_CNT(channel), count & 0xFF);
  50. cpu_write_port_byte(ISA_DMA_CNT(channel), count >> 8);
  51. cpu_write_port_byte(ISA_DMA_MODE_REG(channel), 0x48 | (channel & 3));
  52. cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), (channel & 3));
  53. lock_release(&dma_lock);
  54. }
  55. void isa_dma_write(dword_t channel, void *buffer, dword_t count)
  56. {
  57. channel &= 7;
  58. if (count == 0) return;
  59. else count--;
  60. lock_acquire(&dma_lock);
  61. cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), ISA_DMA_MASK_ON | (channel & 3));
  62. cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
  63. cpu_write_port_byte(ISA_DMA_SAR(channel), (dword_t)buffer & 0xFF);
  64. cpu_write_port_byte(ISA_DMA_SAR(channel), ((dword_t)buffer >> 8) & 0xFF);
  65. cpu_write_port_byte(ISA_DMA_PAR(channel), ((dword_t)buffer >> 16) & 0xFF);
  66. cpu_write_port_byte(ISA_DMA_FF_RESET_REG(channel), 0xFF);
  67. cpu_write_port_byte(ISA_DMA_CNT(channel), count & 0xFF);
  68. cpu_write_port_byte(ISA_DMA_CNT(channel), count >> 8);
  69. cpu_write_port_byte(ISA_DMA_MODE_REG(channel), 0x44 | (channel & 3));
  70. cpu_write_port_byte(ISA_DMA_SINGLE_MASK_REG(channel), (channel & 3));
  71. lock_release(&dma_lock);
  72. }