HI,

I new to linux device drivers.
I am developing HDLC driver.
I dont know how to read and write data in memory mapped address.
Please help.

/*code*/

#define REG_MAP_ADDR 0x50400000

/* Registers */
typedef struct {
u16 MOD;
u16 STATUS;
u16 TBASE;
u16 Tx;
u16 TxBD;
u16 RBASE;
u16 MAX_BUFF_LEN;
u16 Rx;
u16 RxBD;
}reg_map_1ch_t;

typedef struct {
reg_map_1ch_t reg_ch[16];
}reg_map_t;


reg_map_t *reg_map;
unsigned short data=0;
volatile unsigned short *base_addr;

reg_map = ioremap_nocache(REG_MAP_ADDR, sizeof(reg_map_t));

/*now i have to read and write the data of memory mapped address.
Give me idea. I did as follows */

base_addr = (volatile unsigned short *)reg_map;
data = (unsigned short)(*(base_addr));
printk(KERN_ALERT "Before Write Read Data: %d\n", data);

data = 1;
base_addr[0] = data;

data = (unsigned short)(*(base_addr));
printk(KERN_ALERT "After Write Read Data: %d\n", data);

I did Above code in init module, when i am inserting the module, system will reboot.
Whats the error.
i am not getting error, just reboots the system.

Dani AI

Generated

Short diagnosis and the likely cause: inserting a module that treats an MMIO region like normal RAM (casting the ioremap result to a plain pointer and dereferencing it) commonly causes machine-checks or hardware-triggered resets. MMIO must be accessed with the kernel's mapping/reservation APIs and the proper I/O accessors; direct pointer reads/writes (even with volatile) bypass important architecture-specific semantics and can corrupt device state or the bus.

Correct pattern (high level):

  • Reserve the physical region (so the kernel and other drivers don't race for it).
  • Map it with the kernel MMIO mapping helpers.
  • Use the I/O accessors that match register width and ordering (8/16/32-bit variants).
  • Unmap and release the region on module unload.
    Also account for device endianness and use the managed helpers (devm_*) when writing a platform driver.

Minimal example (illustrative — adapt to the target kernel and hardware):

void __iomem *mmio;
size_t len = 0x1000;            /* mapping size from device docs */
phys_addr_t phys = /* platform/board-provided phys addr */;

if (!request_mem_region(phys, len, "hdlc_driver")) { /* handle busy */ }
mmio = ioremap(phys, len);
if (!mmio) { release_mem_region(phys, len); /* handle error */ }

/* read a 16-bit register at offset 0x10 and set bit 0 */
u16 val = ioread16(mmio + 0x10);
iowrite16(val | 0x1, mmio + 0x10);

iounmap(mmio);
release_mem_region(phys, len);

Troubleshooting notes:

  • Inspect dmesg for oops/fault addresses after the reboot. The faulting address points to the offending access.
  • Verify the physical base and size against /proc/iomem or board docs; overlapping regions can cause system-wide problems.
  • Test with reads only first, then small writes; prefer conservative writes (no global resets).
  • Avoid plain volatile pointer arithmetic for __iomem; use (char __iomem *) for offsets.
  • When asking for help include kernel version, exact oops log, and a minimal formatted code sample (as suggested). ’s tone was unhelpful, but the reminder to post readable code is valid.

Recommended Answers

All 3 Replies

If you can't figure out code tags after 60+ posts, what makes you think kernel programming is for you?

If you can't figure out code tags after 60+ posts, what makes you think kernel programming is for you?

who the hell are you, you are not the person to speak about ability.
Ofcourse i am not brillinat than you, but u have some manners.
First learn how to behave.

If you dont know manners, your knowledge is waste.

I know how to use tags, but in hurry i have not used tags.
Thats it.

If you know answer reply to the post else dont blame others.
I think For this purpose u made 5000+ posts.

commented: I couldn't give a shit about the code if you can't be arsed to use code tags. -7

who the hell are you, you are not the person to speak about ability.
Ofcourse i am not brillinat than you, but u have some manners.
First learn how to behave.

If you dont know manners, your knowledge is waste.

I know how to use tags, but in hurry i have not used tags.
Thats it.

If you know answer reply to the post else dont blame others.
I think For this purpose u made 5000+ posts.

They are the forum rules. Doesn't matter if you are in a hurry. If you know how to do it, then do it and you will not get replies such as the above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.